python - Pandas:Groupby 创建具有计数和计数值的表

标签 python pandas pandas-groupby

我的目标很简单,但不确定是否可行。可重现的例子:

你能从这里开始吗:

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

对此:

        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

通过groupby

我可以做到这一点 df.groupby(['score']).agg({'score': np.size}) 但不知道如何创建新列与列值。

最佳答案

我可以复制你的输出

选项 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

选项 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

关于python - Pandas:Groupby 创建具有计数和计数值的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44271659/

相关文章:

python - pandas 匹配两列并创建另一列

python - 显示创建的新网页的链接

python - 如何基于周保存到 .csv 文件中

python - 如何在 Keras 中使用存储在 HDF5 文件中的数据训练神经网络?

python - 从字典列表创建 Dataframe,不使用 pd.concat()

python pandas 奇怪的多行索引,但不是一行

Python(Pandas)在多索引数据框的每个级别上添加小计

python - 如何使用 pandas 计算 Excel 工作表中 if 语句为 true 的次数?

python - Pandas 分组和组合行

python - 类的方法是否应该至少有一个引用实例的参数?