简体   繁体   English

Pandas使用不相等的列来转移数据帧

[英]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. 任何想法都表示赞赏。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM