Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas pivot dataframe with unequal columns

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.

like image 717
dter Avatar asked Oct 19 '22 10:10

dter


1 Answers

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
like image 91
Colonel Beauvel Avatar answered Oct 21 '22 06:10

Colonel Beauvel