根据下表:
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']是创建的新列。那么,如何在单行代码中组合上面的代码?
答案 0 :(得分:2)
我认为您首先需要过滤,然后将输出添加到过滤列:
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
答案 1 :(得分:1)
您可以计算插入并将其粘贴到原始数据框中。笨拙,但不同。
rows = FamilyVsWWTotal['Customer'] == 'Google'
inset = FamilyVsWWTotal[rows].groupby(['date', 'Country'])['Revenues'].sum()
FamilyVsWWTotal.set_index(['date', 'Country'], inplace=True)
rows = FamilyVsWWTotal['Customer'] == 'Google'
FamilyVsWWTotal.loc[rows,'total1'] = inset
FamilyVsWWTotal.reset_index(inplace = True)
# date Country Customer Revenues total1
#0 2011-4-3 USA Exxon 1200 NaN
#1 2011-3-5 CAN Google 1800 1800.0
#2 2011-4-3 USA Google 1000 1000.0
#3 2011-5-7 MEX Microsoft 800 NaN
答案 2 :(得分:1)
FamilyVsWWTotal.assign(total1=FamilyVsWWTotal[FamilyVsWWTotal.Customer.eq('Google')].groupby(['date', 'Country']).Revenues.transform('sum'))
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.assign(
total1=FamilyVsWWTotal[FamilyVsWWTotal.Customer.eq('Google')].groupby(
['date', 'Country']
).Revenues.transform('sum')
)
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