Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to solve pandas multi-column explode issue?

I am trying to explode multi-columns at a time systematically.
Such that:

[1

and I want the final output as:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

I tried

df=df.explode('sauce', 'meal')

but this only provides the first element ( sauce) in this case to be exploded, and the second one was not exploded.

I also tried:

df=df.explode(['sauce', 'meal'])

but this code provides

ValueError: column must be a scalar

error.

I tried this approach, and also this. none worked.

Note: cannot apply to index, there are some none- unique values in the fruits column.

>Solution :

Prior to pandas 1.3.0 use:

df.set_index(['fruits', 'veggies'])[['sauce', 'meal']].apply(pd.Series.explode).reset_index()

Output:

  fruits veggies sauce meal
0     x1      y2     a    d
1     x1      y2     b    e
2     x1      y2     c    f
3     x2      y2     g    k
4     x2      y2     h    l

Many columns? Try:

df.set_index(df.columns.difference(['sauce', 'meal']).tolist())\
  .apply(pd.Series.explode).reset_index()

Output:

  fruits veggies sauce meal
0     x1      y2     a    d
1     x1      y2     b    e
2     x1      y2     c    f
3     x2      y2     g    k
4     x2      y2     h    l
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading