tomp
tomp

Reputation: 653

Chaining melt and groupby in pandas causes integer values to be cast to boolean

I'm trying to reshape the following dataframe:

df = pd.DataFrame({'feature': [True,False,False,True], 
    'id': [1,0,1,2]})

enter image description here

...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.

enter image description here

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')

enter image description here

Any suggestions for achieving the desired dataframe (without the 0 and 1 being cast to boolean) would be greatly appreciated!

Upvotes: 1

Views: 1860

Answers (1)

BENY
BENY

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

Related Questions