1

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.

1
  • Do you need to preserve the index? Commented Feb 19, 2022 at 20:15

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

It works! At least pandas 0.25.0 is needed to work . Once updated it works fine. Thank you!
When you need to preserve the index, thats my case, after explode(), the null values need to be removed. If not, the Dataframe creation crash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.