简体   繁体   English

在 Pandas Dataframe 中创建一个 Total 行

[英]Create a Total row in Pandas Dataframe

So I am trying to create a new row in my dataframe that adds up the sums for only column AB and C and have the means for column D and E. I have tried doing something like:所以我试图在我的数据框中创建一个新行,它仅将 AB 列和 C 列的总和相加,并具有 D 列和 E 列的平均值。我尝试过执行以下操作:

df.loc['totals'] = df['daily budget', 'weekly budget', 'monthly budget'].sum() + df['average spent monthly','average spent yearly'].mean()

It didnt work.它没有用。 Should I try to define each value by doing something like我应该尝试通过做类似的事情来定义每个值

sum1 = df['daily budget'].sum()
sum2 = df['weekly budget'].sum()
sum3 = ....
mean1 =df['average monthly'].mean()
mean2 = ....

And fill in the cell?并填写单元格?

If you want to go with your approach then this should work.如果你想采用你的方法,那么这应该可行。

sum1 = df['daily budget'] + df['weekly budget'] + df['monthly budget']

mean1 = (df['average spent monthly'] + df['average spent yearly'])/2

df['total'] = sum1 + mean1

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM