创建新列以按日期计数未命中/拍摄的镜头



我正在尝试为当前df创建两个额外的列。一列是记录每天的投篮次数,另一列是显示每天尝试投篮的次数。

I have try:

df.groupby("Date")['ShotMade','ShotTaken'].count() 

但是我没有得到想要的输出。提前感谢!

这是我当前的数据帧:

Date  ShotMade  ShotTaken
4/20/2020         0          1
4/20/2020         0          1
4/20/2020         1          1
4/20/2020         1          1
4/21/2020         1          1
4/21/2020         1          1
4/21/2020         1          1
4/22/2020         1          1
4/22/2020         1          1
4/22/2020         0          1
4/22/2020         0          1
4/22/2020         0          1

所需输出:

Date  ShotMade  ShotTaken
4/20/2020         2          4
4/21/2020         3          3
4/22/2020         2          5

使用groupby_sum而不是groupby_count来对每组的值求和,而不是计数项目。使用[[ ]]而不是[ ]来选择列,否则Pandas将引发FutureWarning:

out = df.groupby('Date', as_index=False)[['ShotMade', 'ShotTaken']].sum()
print(out)
# Output
Date  ShotMade  ShotTaken
4/20/2020         2          4
4/21/2020         3          3
4/22/2020         2          5