Reputation: 653
I'm trying to reshape the following dataframe:
df = pd.DataFrame({'feature': [True,False,False,True],
'id': [1,0,1,2]})
...to create a dataframe similar to the example below. Column names should now be the index, and the frequency of each unique value should be provided as a count.
Using melt and groupby almost achieves this, except that 0 and 1 (integers) are cast to False and True (boolean).
df.melt().groupby(['variable','value']).size().to_frame(name='freq')
Any suggestions for achieving the desired dataframe (without the 0 and 1 being cast to boolean) would be greatly appreciated!
Upvotes: 1
Views: 1860
Reputation: 323346
Using dtype
convert id
to str
df.id=df.id.astype(str)
df.melt().groupby(['variable','value']).size().to_frame(name='freq')
Out[81]:
freq
variable value
feature False 2
True 2
id 0 1
1 2
2 1
Upvotes: 1