dter
dter

Reputation: 1165

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.

Upvotes: 6

Views: 1476

Answers (1)

Colonel Beauvel
Colonel Beauvel

Reputation: 31171

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

Upvotes: 5

Related Questions