python - 使用条件总和的结果创建 Pandas DataFrame 列

标签 python pandas dataframe conditional-statements

相关于this关于根据条件计算 DataFrame 值的问题,我有一个更复杂的问题,关于包含基于我正在处理的给定行的条件的总和。这是初始 df:

Key UID VID count   month   option  unit    year
0   1   5   100     1       A       10      2015
1   1   5   200     1       B       20      2015
2   1   5   300     2       A       30      2015
3   1   5   400     2       B       40      2015
4   1   7   450     2       B       45      2015
5   1   5   500     3       B       50      2015

我希望迭代这个时间序列 DataFrame,为每一行添加一列“unit_count”,仅在选项为“B”的情况下将该月的“unit”值除以“count”总和。本质上:

df['unit_count'] = df['unit'] / sum of df['count'] for all records containing 'option' 'B' in the same month

这将按如下方式附加 DataFrame:

Key UID VID count   month   option  unit    year    unit_count
0   1   5   100     1       A       10      2015    0.050
1   1   5   200     1       B       20      2015    0.100
2   1   5   300     2       A       30      2015    0.035
3   1   5   400     2       B       40      2015    0.047
4   1   7   450     2       B       45      2015    0.053
5   1   5   500     3       B       50      2015    0.100

上面示例 df 的代码是:

df = pd.DataFrame({'UID':[1,1,1,1,1,1],
                   'VID':[5,5,5,5,7,5],
                'year':[2015,2015,2015,2015,2015,2015],
                'month':[1,1,2,2,2,3],
                'option':['A','B','A','B','B','B'],
                'unit':[10,20,30,40,45,50],
                'count':[100,200,300,400,450,500]
                })

最佳答案

只想查看同一个月,因此您可以按 month 列进行分组,然后在每个组中使用 option == "B" 来子集count 列并求和,使用求和值除 unit 列(逻辑的翻译):

df['unit_count'] = df.groupby('month', group_keys=False).apply(
                      lambda g: g.unit/g['count'][g.option == "B"].sum())
df

enter image description here

关于python - 使用条件总和的结果创建 Pandas DataFrame 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42011948/

相关文章:

python - Pandas - get_dummies 与选定的集合

python - 具有混合类型的 Pandas DataFrame 样式会产生 TypeError

pandas - 从 Pandas 数据框中的字符串日期获取一年中的哪一天

python - 没有聚合的 Pandas 数据透视表形状

python - 无法连接 Keras Lambda 层

python - python字符串格式缺少一个空格

Python按索引连接数据框

python - 将json数据读入DataFrame

python - 使用 Python 命名空间包进行开发

python - 导出 Scikit Learn 随机森林以在 Hadoop 平台上使用