英文:
Trying to sum a column, but only if the value corresponds to a value in another column (excel doc imported to Jupyter)
问题
我正在从Excel导入一个表格,并尝试对列进行求和,但是要根据另一列进行筛选。
所以,根据参考图片,我想要求解'R-AMT'列的总和,但是只针对'HIT'列中值为'Done'的单元格。
我对编程非常新手,所以没有太多的例子。我尝试了groupby
函数,但它返回了'Done'和'Away'的值,这并不有用。
hit_volume = df.groupby('HIT')[['R-AMT']].sum()
非常感谢您的帮助!
英文:
I am importing a sheet from Excel, and trying to sum the columns but filter them based on another column.
So, using the picture for reference, I would like the sum of the R-AMT column, but only for those cells in the 'HIT' column with a 'Done' value.
I am very new to coding, so I don't have many examples. I have tried the groupby
function but it returned both Done and Away values, which wasn't useful.
hit_volume = df.groupby('HIT')[['R-AMT']].sum()
Thank you in advance for your help!
答案1
得分: 2
df.loc[df['HIT'] == 'Done', 'R-AMT'].sum()
英文:
df.loc[df['HIT'] == 'Done', 'R-AMT'].sum()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论