Search code examples
pythonpandasdataframepandas-explode

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:

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