简体   繁体   English

Pandas:Groupby用于创建具有计数和计数值的表

[英]Pandas: Groupby to create table with count and count values

My objective is simple but not sure if it's possible. 我的目标很简单,但不确定是否可行。 Reproducible example: 可重复的例子:

Can you go from this: 你能不能这样做:

raw_data = {'score': [1, 3, 4, 4, 1, 2, 2, 4, 4, 2],
        'player': ['Miller', 'Jacobson', 'Ali', 'George', 'Cooze', 'Wilkinson', 'Lewis', 'Lewis', 'Lewis', 'Jacobson']}
df = pd.DataFrame(raw_data, columns = ['score', 'player'])
df

    score   player
0   1       Miller
1   3       Jacobson
2   4       Ali
3   4       George
4   1       Cooze
5   2       Wilkinson
6   2       Lewis
7   4       Lewis
8   4       Lewis
9   2       Jacobson

To this: 对此:

        score    col_1       col_2       col_3       col_4     
score   
1       2        Miller      Cooze       n/a         n/a
2       3        Wilkinson   Lewis       Jacobson    n/a
3       1        Jacobson    n/a         n/a         n/a
4       4        Ali         George      Lewis       Lewis

Via a groupby ? 通过groupby

I can get this far df.groupby(['score']).agg({'score': np.size}) but can't work out how to create the new columns with the column values. 我可以得到这个远df.groupby(['score']).agg({'score': np.size})但无法弄清楚如何使用列值创建新列。

I can duplicate your output with 我可以复制你的输出

Option 1 选项1

g = df.groupby('score').player
g.size().to_frame('score').join(g.apply(list).apply(pd.Series).add_prefix('col_'))

       score      col_0   col_1     col_2  col_3
score                                           
1          2     Miller   Cooze       NaN    NaN
2          3  Wilkinson   Lewis  Jacobson    NaN
3          1   Jacobson     NaN       NaN    NaN
4          4        Ali  George     Lewis  Lewis

Option 2 选项2

d1 = df.groupby('score').agg({'score': 'size', 'player': lambda x: tuple(x)})
d1.join(pd.DataFrame(d1.pop('player').values.tolist()).add_prefix('col_'))

       score      col_0   col_1     col_2  col_3
score                                           
1          2     Miller   Cooze       NaN    NaN
2          3  Wilkinson   Lewis  Jacobson    NaN
3          1   Jacobson     NaN       NaN    NaN
4          4        Ali  George     Lewis  Lewis

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

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