I have a Pandas dataframe where the columns are 'month' and 'year', and a 'value_list' which is a list of values - one for each day of the month. Something like this -
| year | month | value_list |
|---|---|---|
| 1990 | 2 | [10, 20, 30, 40, ...... 290, 300] |
| 1990 | 3 | [110, 120, 130, 140, ...... 390, 400] |
I want to unpack these into multiple rows - one for each day (new column), so that I get something like the following -
| year | month | day | value |
|---|---|---|---|
| 1990 | 2 | 1 | 10 |
| 1990 | 2 | 2 | 20 |
| 1990 | 2 | 3 | 30 |
| 1990 | 2 | 4 | 40 |
and so on.
I tried using df.explode() but to no avail since the index gets reset or is set to a new single index. How can I automatically get the dates (essentially creating a year, month, date multiindex) while unpacking the lists?
df.explode('value_list')should work ifdf['value_list']is a true list. If not working, trydf['value_list']=df['value_list'].str.strip('[]').str.split(',')followed bydf.explode('value_list')