简体   繁体   English

Python - Pandas DF - 对与另一列中的条件匹配的列中的值求和

[英]Python - Pandas DF - sum values in a column that match a condition in another column

I would like to sum values in one column based on a condition in another column.我想根据另一列中的条件对一列中的值求和。 I can do this when the condition exists, but if it does not, I get an error.当条件存在时我可以这样做,但如果不存在,我会收到错误消息。 I need this to accept that condition doesn't exist and move on to the next step.我需要这个来接受条件不存在并继续下一步。

Example df:示例 df:

import pandas as pd
technologies   = ({
    'Courses':["Spark","PySpark","Hadoop","Python","Pandas","Hadoop","Spark","Python"],
    'Fee' :[22000,25000,23000,24000,26000,25000,25000,22000],
    'Duration':['30days','50days','55days','40days','60days','35days','55days','50days']
                })
df = pd.DataFrame(technologies, columns=['Courses','Fee','Duration'])
print(df)
Courses    Fee Duration
0    Spark  22000   30days
1  PySpark  25000   50days
2   Hadoop  23000   55days
3   Python  24000   40days
4   Pandas  26000   60days
5   Hadoop  25000   35days
6    Spark  25000   55days
7   Python  22000   50days

for this example, I would like to sum the fee for all lines that have "55days"对于这个例子,我想总结所有有“55days”的线路的费用

duration = df.groupby('Duration')['Fee'].sum()["55days"]
print (df)
48000

# but if I choose a value that does not appear under Duration like "22days" I get an error #但是如果我选择了一个没有出现在 Duration 下的值,比如“22days”,我会得到一个错误

duration22 = df.groupby('Duration')['Fee'].sum()["22days"]

Can you please advise how I can code this so if the value "22days" happens not to exist on this run it does not fail or it just puts a 0 value in if null?你能告诉我如何编码,所以如果值“22days”在这次运行中碰巧不存在,它不会失败,或者它只是在 null 时输入一个 0 值?

You could do a pre-lookup check in the grouped index.您可以在分组索引中进行预查找检查。

gd_sum = df.groupby('Duration')['Fee'].sum()

def dur_sum(k):
    return gd_sum[k] if k in gd_sum.index else 0


print(dur_sum('55days'))
48000

print(dur_sum('22days'))
0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Pandas DataFrame - 如何根据另一列(日期类型)中的部分匹配对 1 列中的值求和? - Python Pandas DataFrame - How to sum values in 1 column based on partial match in another column (date type)? Python Pandas - 如何将 df 中的列值与另一个 df 的列值进行比较 - Python Pandas - How to compare the column values in a df with column values of another df 如果值相同,Python Pandas会将列从df复制到另一个 - Python Pandas copying column from df to another if values same Pandas df:用另一列中的特定值填充新列中的值(具有多列的条件) - Pandas df: fill values in new column with specific values from another column (condition with multiple columns) python pandas:如果条件,则删除一个df列 - python pandas: drop a df column if condition 熊猫df。 将一个数据帧中的列的值与另一数据帧中的列的值进行匹配 - Pandas df. Match values of a column from one dataframe with a values from a column from another dataframe pandas:使用与另一个 df 中的索引和列匹配的值填充 df 列 - pandas: populate df column with values matching index and column in another df 如何用条件 pandas python 替换另一列中的列的值 - how to replace the values of a column from another column with condition pandas python 当条件为真时如何对 pandas 列的值求和(Python) - How to sum the values of a pandas column when a condition is true (Python) 基于另一个 DF 替换 pandas 列值 - Replace pandas column values based on another DF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM