The data I have right now is structured as lists in a single row of a dataframe and I'd like to break it into multiple rows. What I'm working with now:
and the ideal outcome would be:
0 CCDC160 CYTH3 ......
1 6.5656 8.9028
2 6.2135 7.9683 .......
. . .
. . .
1000 5.6789 6.6787 ......
multiple splits into arrays were not working out for me.
CodePudding user response:
You could use explode to explode list to rows. See example below.
import pandas as pd
df = pd.DataFrame([['abc', 'def'],[[1,2,3],[4,5,6]]], columns=['0','1'])
print(df)
print(df.explode(['0','1']))
Output:
0 1
0 abc def
1 [1, 2, 3] [4, 5, 6]
0 1
0 abc def
1 1 4
1 2 5
1 3 6
CodePudding user response:
Try using pd.concat
:
new_df = pd.concat([df.drop('SeqData'), pd.DataFrame(df.loc['SeqData'].tolist()).T])
CodePudding user response:
Based on Manjunath K Mayya post, I added reset_index so row index is reset.
import pandas as pd
df = pd.DataFrame([['abc', 'def'],[[1,2,3],[4,5,6]]], columns=['0','1'])
print(df)
print(df.explode(['0','1']).reset_index(drop=True))
Output:
0 1
0 abc def
1 [1, 2, 3] [4, 5, 6]
0 1
0 abc def
1 1 4
2 2 5
3 3 6