How do I sort data from a DataFrame into a DataFrame that uses a MultiIndex for both the indices and columns?
For example, convert from this:
0 1 2 3 4
0 foo two A 2.30 0.01
1 foo one A 4.12 0.13
2 bar two B 9.89 3.66
3 foo one A 2.11 9.48
4 bar two A 1.07 5.55
to this:
A B
1 2 1 2
foo one 2.11 9.48 NaN NaN
two 2.3 0.01 NaN NaN
bar one NaN NaN NaN NaN
two 1.07 5.55 9.89 3.66
Currently I am iterating over each row in df1
and updating the values in df2
, but I'd like a more efficient method than this:
for index, row in df1.iterrows():
df2.loc[(row[0], row[1]), row[2]] = list(row[3:])