I have dataframe like this:
id Gender TV Radio
0 M Daily Daily
1 F Monthly Weekly
2 M Weekly Weekly
3 F Daily Daily
I need to change the columns into values and values into column, This is my desired output.
id Gender Daily Monthly Weekly
0 M TV NaN NaN
0 M Radio NaN NaN
1 F NaN TV Weekly
2 M NaN NaN TV
2 M NaN NaN Radio
3 F TV NaN NaN
3 F Radio NaN NaN
I am using melt+pivot but i lose information. Here is what i am using.
idx = ['id', 'Gender']
m = df.melt(idx).pivot_table(index=idx,columns='value',values='variable',aggfunc='first')
out = m.reset_index(idx).rename_axis(index=None,columns=None)
But i am getting only the first value. For example if the daily occur for tv and radio then i am geting only the first value.. I know i am aggregating only first but dont know which function i can use to get all the values.
id Gender Daily Monthly Weekly
0 M TV NaN NaN
1 F NaN TV Weekly
2 M NaN NaN TV
3 F TV NaN NaN