python - 如何在一行 python pandas 代码中应用 2 个不同的条件?

标签 python pandas pandas-groupby

根据下表:

FamilyVsWWTotal = pd.DataFrame({'date':['2011-4-3','2011-3-5','2011-4-3','2011-5-7'], 'Country':['USA','CAN','USA','MEX'],'Revenues':[1200,1800,1000,800],'Customer':['Exxon','Google','Google','Microsoft']})

FamilyVsWWTotal = FamilyVsWWTotal.loc[FamilyVsWWTotal['Customer'] == 'Google']
FamilyVsWWTotal['total1'] = FamilyVsWWTotal.groupby(['date','Country'])['Revenues']\
                                           .transform('sum')

FamilyVsWWTotal['total1'] 是创建的新列。那么,如何将上面的代码组合成一行代码呢?

最佳答案

我认为您首先需要过滤,然后也将输出添加到过滤列:

m = FamilyVsWWTotal['Customer'] == 'Google'

FamilyVsWWTotal.loc[m, 'total1']=FamilyVsWWTotal[m].groupby(['date','Country'])['Revenues']\
                                                   .transform('sum')

print (FamilyVsWWTotal)
  Country   Customer  Revenues      date  total1
0     USA      Exxon      1200  2011-4-3     NaN
1     CAN     Google      1800  2011-3-5  1800.0
2     USA     Google      1000  2011-4-3  1000.0
3     MEX  Microsoft       800  2011-5-7     NaN

对于一行解决方案:

FamilyVsWWTotal.loc[FamilyVsWWTotal['Customer'] == 'Google', 'total1'] = \
FamilyVsWWTotal[FamilyVsWWTotal['Customer']=='Google'].groupby(['date','Country'])['Revenues']\
                                                      .transform('sum')

print (FamilyVsWWTotal)
  Country   Customer  Revenues      date  total1
0     USA      Exxon      1200  2011-4-3     NaN
1     CAN     Google      1800  2011-3-5  1800.0
2     USA     Google      1000  2011-4-3  1000.0
3     MEX  Microsoft       800  2011-5-7     NaN

关于python - 如何在一行 python pandas 代码中应用 2 个不同的条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45667307/

相关文章:

python - Python 缩写检测

Python:如何在用分号分隔的 Pandas 数据框的列中查找值?

python - 我想根据条件将 Pandas Dataframe 分成 2 个数据帧

python - matplotlib scatter 失败并出现错误 : 'c' argument has n elements,,这对于大小为 n 的 'x'、大小为 n 的 'y' 来说是 Not Acceptable

python: 是否可以检测我是否在 Paster shell 中?

python - PyQT5 QSystemTrayIcon 激活信号不起作用

python 按列分组并按层次结构选择

python - 基于现有列向 Pandas DataFrame 添加多个列

pandas split-apply-combine 创建不需要的多重索引

python - Pandas :分组依据和数据透视表的区别