I have a data set structured like this:
mydic = {'2017-9-11': {'Type1': [15, 115452.0, 3], 'Type2': [47, 176153.0, 4], 'Type3': [0, 0, 0]}, '2017-9-12': {'Type1': [26, 198223.0, 5], 'Type2': [39, 178610.0, 6], 'Type3': [0, 0, 0]}}
df = pd.DataFrame.from_dict(mydic, orient='index')
I need to split values in the lists into different columns and group them by Types. This is what I do:
df_new = df[list(df)].unstack().apply(pd.Series)
df_new.head()
And it works:
0 1 2
Type1 2017-9-11 15.0 115452.0 3.0
2017-9-12 26.0 198223.0 5.0
Type3 2017-9-11 0.0 0.0 0.0
2017-9-12 0.0 0.0 0.0
Type2 2017-9-11 47.0 176153.0 4.0
BUT when I apply this code to a larger real-life data set it seems like apply(pd.Series)
doesn't work and I get just one column 0 with lists of values like this:
0
Type1 2017-9-11 [15, 115452.0, 3]
2017-9-12 [26, 198223.0, 5]
Type2 2017-9-11 [47, 176153.0, 4]
2017-9-12 [39, 178610.0, 6]
Type3 2017-9-11 [0, 0, 0]
Can anyone suggest what might be wrong? Or suggest an alternative solution?