[英]Sum of only certain columns in a pandas Dataframe
I have a dataframe similar to the one below.我有一个类似于下面的 dataframe。 I need to add up the sum of only certain columns: Jan-16, Feb-16, Mar-16, Apr-16 and May-16.我只需要将某些列的总和相加:Jan-16、Feb-16、Mar-16、Apr-16 和 May-16。 I have these columns in a list called months_list我在一个名为months_list的列表中有这些列
--------------------------------------------------------------------------------------
| Id | Name | Jan-16 | Feb-16 | Mar-16 | Apr-16 | May-16 |
| 4674393 | John Miller | 0 | 1 | 1 | 1 | 1 |
| 4674395 | Joe Smith | 0 | 0 | 1 | 1 | 1 |
---------------------------------------------------------------------------------------
My output should look like the below:我的 output 应该如下所示:
--------------------------------------------------------------------------------------
| Id | Name | Jan-16 | Feb-16 | Mar-16 | Apr-16 | May-16 |
| 4674393 | John Miller | 0 | 1 | 1 | 1 | 1 |
| 4674395 | Joe Smith | 0 | 0 | 1 | 1 | 1 |
|Total | | 0 | 1 | 2 | 2 | 2 |
---------------------------------------------------------------------------------------
A new row called 'Total' should be introduced with a column wise sum for all the columns in my months_list: Jan-16, Feb-16, Mar-16, Apr-16 and May-16应该为我的months_list中的所有列引入一个名为“Total”的新行,并按列求和:Jan-16、Feb-16、Mar-16、Apr-16 和 May-16
I tried the below and it did not work.我尝试了以下方法,但没有成功。 I got all NaN values我得到了所有的 NaN 值
df.loc['Total',:]= df[months_list].sum(axis=1)
You are using the wrong value of axis
parameter.您使用了错误的axis
参数值。
`axis=0`: Sums the column values
`axis=1`: Sums the row values
Assuming your df to be:假设您的 df 为:
In [4]: df
Out[4]:
Id Name Jan-16 Feb-16 Mar-16 Apr-16 May-16
0 4674393 John Miller 0 1 1 1 1
1 4674395 Joe Smith 0 0 1 1 1
In [10]: months_list =['Jan-16', 'Feb-16', 'Mar-16', 'Apr-16', 'May-16']
You code should be:你的代码应该是:
In [12]: df.loc['Total'] = df[months_list].sum()
In [13]: df
Out[13]:
Id Name Jan-16 Feb-16 Mar-16 Apr-16 May-16
0 4674393.0 John Miller 0.0 1.0 1.0 1.0 1.0
1 4674395.0 Joe Smith 0.0 0.0 1.0 1.0 1.0
Total NaN NaN 0.0 1.0 2.0 2.0 2.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.