I have a dataframe containing a categorical variable in one column and a continuous variable in another column like so:
gender contVar
Male 22379
Female 24523
Female 23421
Male 23831
Male 29234
I want to get a table like so:
Male Female
22379 24523
23831 23421
23831
29234
Is this possible in pandas? When I do:
df.pivot(index = df.index.tolist(), columns='gender', values='contVar')
I get that the index is out of bounds (obviously since there arent rows as there are indices but I also presume that its because the number of rows in each column are not equal). Any ideas are appreciated.
You can do:
pd.concat([pd.DataFrame({g:d.contVar.tolist()}) for g,d in df.groupby('gender')], axis=1)
Out[416]:
Female Male
0 24523 22379
1 23421 23831
2 NaN 29234
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With