python - 如何使用多级索引 pandas 数据框中的列的总和值作为新列中的值的条件

标签 python pandas where-clause multi-level

我有一个多级索引 pandas 数据框。我想创建一个新列,其中该列中的值基于条件。此条件基于对该索引的另一列求和,然后将其减半。如果该值小于存储在单独列表中的最后一个值,则新列中的值将采用与数据帧中另一列相同的值。如果不满足此条件,则新列中的所有值都应为 0

利用这个问题来尝试实现这个目标Sum columns by level in a Multi-Index DataFrame我使用了 np.wheredf.sum(level=0, axis=1) 的组合,但这会导致以下错误:

ValueError: operands could not be broadcast together with shapes (2,8) (21,) ()

这是我的数据框和我迄今为止使用的代码的示例:

import pandas as pd
import numpy as np

balance = [1400]

data = {'EVENT_ID': [112335580,112335580,112335580,112335580,112335580,112335580,112335580,112335580, 112335582,
                     112335582,112335582,112335582,112335582,112335582,112335582,112335582,112335582,112335582,
                     112335582,112335582,112335582],

 'SELECTION_ID': [6356576,2554439,2503211,6297034,4233251,2522967,5284417,7660920,8112876,7546023,8175276,8145908,
                  8175274,7300754,8065540,8175275,8106158,8086265,2291406,8065533,8125015],

 'Pot_Bet': [3.236731,2.416966,2.278365,2.264023,2.225353,2.174407, 2.141420,2.122386,2.832997,2.411094,
         2.167218,2.138972,2.132137,2.128341,2.116338,2.115239,2.115123,2.114284362,2.113420,
         2.113186,2.112729],

  'Liability':[3.236731, 2.416966, 12.245492, 12.795112, 15.079176, 23.336171, 50.741182, 571.003118, 2.832997, 6.691736, 15.808607, 27.935834, 35.954927, 43.275250, 147.165537, 193.017915, 199.622454, 265.809019, 405.808678, 473.926781, 706.332594]}

df = pd.DataFrame(data, columns=['EVENT_ID', 'SELECTION_ID', 'Pot_Bet','WIN_LOSE'])

df.set_index(['EVENT_ID', 'SELECTION_ID'], inplace=True) #Selecting columns for indexing

df['Bet'] = np.where(df.sum(level = 0) > 0.5*balance[-1], df['Pot_Bet'], 0)

这会导致前面所述的错误。

对于索引 112335580,新列应具有与 'Pot_Bet' 相同的值。而对于索引 112335582,新列的值应为 0

干杯, 桑迪

最佳答案

问题是如果使用df.sum(level=0),它与df.groupby(level = 0).sum()相同 - 按第一级聚合MultiIndex

解决方案是使用GroupBy.transform对于与原始 DataFrame 大小相同的 Series:

df['Bet'] = np.where(df.groupby(level = 0)['Pot_Bet'].transform('sum') > 0.5*balance[-1], 
                     df['Pot_Bet'], 0)

详细信息:

print (df.groupby(level = 0)['Pot_Bet'].transform('sum'))
EVENT_ID   SELECTION_ID
112335580  6356576         18.859651
           2554439         18.859651
           2503211         18.859651
           6297034         18.859651
           4233251         18.859651
           2522967         18.859651
           5284417         18.859651
           7660920         18.859651
112335582  8112876         28.611078
           7546023         28.611078
           8175276         28.611078
           8145908         28.611078
           8175274         28.611078
           7300754         28.611078
           8065540         28.611078
           8175275         28.611078
           8106158         28.611078
           8086265         28.611078
           2291406         28.611078
           8065533         28.611078
           8125015         28.611078
Name: Pot_Bet, dtype: float64

如果只需要使用磨练列,则可以通过列名称将其选择为系列:

print (df['Pot_Bet'].sum(level=0))
EVENT_ID
112335580    18.859651
112335582    28.611078
Name: Pot_Bet, dtype: float64

print (df.groupby(level = 0)['Pot_Bet'].sum())
EVENT_ID
112335580    18.859651
112335582    28.611078
Name: Pot_Bet, dtype: float64

关于python - 如何使用多级索引 pandas 数据框中的列的总和值作为新列中的值的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55431600/

相关文章:

Python 计算两列中的不同值

python - 在程序生命周期中替换 print func 换行符

Python-撤消标准输出重定向

python 1 :1 stratified sampling per each group

python - 类型错误 : wrapper() takes 1 positional argument but 2 were given

python - Pandas 相似度计算中的序数变量处理

python - Pandas - 计算字符字段中逗号的数量

sql - 在 where 子句中运行返回 boolean 值的 SQL 用户定义函数

php - 我搞砸了我的where子句吗?得到意想不到的结果

SQL Read Where IN(来自 .TXT 文件的长列表)