Miya Wang Miya Wang - 1 year ago 237
Python Question

unstack a multiindex dataframe

I want to unstack a multi-index dataframe, which looks like this:

original dataframe

into another dataframe whose index is 'Worker_id', column names are 'Task_id' and values are 'Date_cnt'.

Could someone give a help?

I've tried df.unstack, but it automatically puts 'Date_cnt',rather than 'Task_id' as column names


Answer Source

I think this is what you want:

import pandas as pd

df = pd.DataFrame([[4529,338,6],[4529,340,4],[4529,346,4],[4529,388,4],[4529,824,1]], columns = ['Worker_id','Task_id','Date_cnt'])
df = df.set_index(['Worker_id','Task_id']).unstack()
df.columns = df.columns.droplevel()
print df

Task_id    338  340  346  388  824
4529         6    4    4    4    1

Because there is only one column, the Date_cnt is the very top field in the columns multiindex- if you had multiple columns before unstacking, they would all be at the very top. Since you don't want to keep that, you can just drop the column.