Python:按组计算数据框中的特定事件

标签 python pandas dataframe count

假设我有一个 df:

df = pd.DataFrame({'id': [12, 35, 37, 67, 99, 78],
                  'product': ['banana', 'apple', 'banana', 'pear', 'banana', 'apple'],
                  'reordered': [1, 0, 0, 1, 1, 1]})


    id     product   reordered
0   12     banana    1
1   35     apple     0
2   37     banana    0
3   67     pear      1
4   99     banana    1
5   78     apple     1

我想计算“产品”列中产品出现的次数,以及按产品分组的“重新排序”列中的值。期望的结果:

       product   count   reordered_0   reordered_1
   0   banana    3       1             2
   1   apple     2       1             1
   2   pear      1       1             0

请多多指教

最佳答案

使用crosstabDataFrame.insert对于第一个位置的列:

df = pd.crosstab(df['product'], df.reordered).add_prefix('reordered_')
df.insert(0, 'count', df.sum(axis=1))
df = df.reset_index().rename_axis(None, axis=1)
print(df)
  product  count  reordered_0  reordered_1
0   apple      2            1            1
1  banana      3            1            2
2    pear      1            0            1

关于Python:按组计算数据框中的特定事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67597093/

相关文章:

类中的 Python 装饰器

python - 对 pandas 中的边求和

python - 从字典列表创建 Pandas 数据框,字典键作为列

python - 比较 2 个数据框的列

python - 返回多列中 pandas 的所有重复项

python - 在特定时间步重置数据帧列中的值并减去行

python - 由于焦点位于前一帧的条目,Tkinter 绑定(bind)方法不起作用

python - 在python中,如果一个函数没有return语句,它会返回什么?

python - 为什么对 ndarray 进行切片会 reshape 它的形状?

python - 在 Pandas 面板中选择行