0

I have a data frame in this format, but with hundreds more rows and columns:

df = pd.DataFrame({'col1': ['A', 'B', 'C'],
                        'col2': ['1, 5, 10', '1, 10, 20', '1, 5, 10'],
                      'col3': ['2, 10, 20', '2, 10, 20', 'None']})

Instead of having so many columns, I want to rearrange the df so each value in col1 is duplicated across many rows, and rearrange the values in this way:

dfoutput = pd.DataFrame({'col1': ['A', 'A', 'B', 'B', 'C'],
         'col2': ['1, 5, 10', '2, 10, 20', '1, 10, 20', '2, 10, 20', '1, 5, 10']})

I tried using combinations of df.melt and df.groupby but I'm getting nowhere. I also found this solution, but I have hundreds of columns to split instead of 1 (and they aren't tuples). I also fiddled with the answer given here, but the df organization is different enough that I couldn't get it to work for my df.

Liquidity
  • 625
  • 6
  • 24

0 Answers0