Home > Net >  python pandas extract column of array to rows and colums
python pandas extract column of array to rows and colums

Time:02-20

Hello I have a dataframe with the following structure:

Index   A   B   C   D   E
3    foo    bar 0   1   [{'A': 1, 'B': 'text', 'C': 3}, {'A': 2, 'B': 'other', 'C': 2}]
4    foo    bar 0   1   [{'A': 3, 'B': 'foo', 'C': 4}, {'A': 6, 'B': 'bar', 'C': 8}]

With loc I get the Index and the E column

df2 = df.loc[:, ['E']]

Index   E
3   [{'A': 1, 'B': 'text', 'C': 3}, {'A': 2, 'B': 'other', 'C': 2}]
4   [{'A': 3, 'B': 'foo', 'C': 4}, {'A': 6, 'B': 'bar, 'C': 8}]

But what I need is this structure

Index   A   B   C
3   1   text    3
3   2   other   2
4   3   foo 4
4   6   bar 8

I think that iterating over the rows, extracting the array and creating another df for each will work but I hope that more efficient solution can be found.

Thanks in advance.

CodePudding user response:

You can use explode to flatten your column then create a dataframe:

out = pd.DataFrame(df['E'].explode().tolist())
print(out)

# Output
   A      B  C
0  1   text  3
1  2  other  2
2  3    foo  4
3  6    bar  8

To preserve the index:

out = df['E'].explode()
out = pd.DataFrame(out.tolist(), index=out.index)
print(out)

# Output
   A      B  C
3  1   text  3
3  2  other  2
4  3    foo  4
4  6    bar  8
  • Related