Trying to sum a column, but only if the value corresponds to a value in another column (excel doc imported to Jupyter)

huangapple go评论151阅读模式
英文:

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()

非常感谢您的帮助!

Trying to sum a column, but only if the value corresponds to a value in another column (excel doc imported to Jupyter)

英文:

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!

Trying to sum a column, but only if the value corresponds to a value in another column (excel doc imported to Jupyter)

答案1

得分: 2

df.loc[df['HIT'] == 'Done', 'R-AMT'].sum()

英文:
df.loc[df['HIT'] == 'Done', 'R-AMT'].sum()

huangapple
  • 本文由 发表于 2023年8月8日 21:13:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定