如何获得 Pandas 列的总数

我有一个 Pandas 数据帧,如下所示,数据帧有多个列,我想获得列的总数,MyColumn

print df

           X           MyColumn      Y              Z
0          A           84         13.0           69.0
1          B           76         77.0          127.0
2          C           28         69.0           16.0
3          D           28         28.0           31.0
4          E           19         20.0           85.0
5          F           84        193.0           70.0

<强> < / >强我尝试:

我已经尝试使用groupby.sum()来获得列的和:

Total = df.groupby['MyColumn'].sum()


print Total

这将导致以下错误:

TypeError: 'instancemethod' object has no attribute '__getitem__'

< >强预期输出< / >强

我原本期望输出如下:

319

或者,我希望用一个名为TOTAL的新行编辑df,其中包含总数:

           X           MyColumn      Y              Z
0          A           84         13.0           69.0
1          B           76         77.0          127.0
2          C           28         69.0           16.0
3          D           28         28.0           31.0
4          E           19         20.0           85.0
5          F           84        193.0           70.0
TOTAL                  319
528540 次浏览

你应该使用sum:

Total = df['MyColumn'].sum()
print(Total)
319

然后你使用locSeries,在这种情况下,索引应该设置为与你需要求和的特定列相同:

df.loc['Total'] = pd.Series(df['MyColumn'].sum(), index=['MyColumn'])
print(df)
X  MyColumn      Y      Z
0        A      84.0   13.0   69.0
1        B      76.0   77.0  127.0
2        C      28.0   69.0   16.0
3        D      28.0   28.0   31.0
4        E      19.0   20.0   85.0
5        F      84.0  193.0   70.0
Total  NaN     319.0    NaN    NaN

因为如果你传递标量,所有行的值都将被填充:

df.loc['Total'] = df['MyColumn'].sum()
print(df)
X  MyColumn      Y      Z
0        A        84   13.0   69.0
1        B        76   77.0  127.0
2        C        28   69.0   16.0
3        D        28   28.0   31.0
4        E        19   20.0   85.0
5        F        84  193.0   70.0
Total  319       319  319.0  319.0

另外两个解决方案是使用atix,参见下面的应用:

df.at['Total', 'MyColumn'] = df['MyColumn'].sum()
print(df)
X  MyColumn      Y      Z
0        A      84.0   13.0   69.0
1        B      76.0   77.0  127.0
2        C      28.0   69.0   16.0
3        D      28.0   28.0   31.0
4        E      19.0   20.0   85.0
5        F      84.0  193.0   70.0
Total  NaN     319.0    NaN    NaN

df.ix['Total', 'MyColumn'] = df['MyColumn'].sum()
print(df)
X  MyColumn      Y      Z
0        A      84.0   13.0   69.0
1        B      76.0   77.0  127.0
2        C      28.0   69.0   16.0
3        D      28.0   28.0   31.0
4        E      19.0   20.0   85.0
5        F      84.0  193.0   70.0
Total  NaN     319.0    NaN    NaN

自Pandas v0.20以来,ix已弃用。使用lociloc代替。

另一种选择是:

df.loc["Total", "MyColumn"] = df.MyColumn.sum()


#         X  MyColumn      Y       Z
#0        A     84.0    13.0    69.0
#1        B     76.0    77.0   127.0
#2        C     28.0    69.0    16.0
#3        D     28.0    28.0    31.0
#4        E     19.0    20.0    85.0
#5        F     84.0   193.0    70.0
#Total  NaN    319.0     NaN     NaN

你也可以使用append()方法:

df.append(pd.DataFrame(df.MyColumn.sum(), index = ["Total"], columns=["MyColumn"]))

enter image description here


更新:

如果你需要为所有数字列附加sum,你可以执行以下操作之一:

使用append以函数式方式完成此操作(不改变原始数据帧):

# select numeric columns and calculate the sums
sums = df.select_dtypes(pd.np.number).sum().rename('total')


# append sums to the data frame
df.append(sums)
#         X  MyColumn      Y      Z
#0        A      84.0   13.0   69.0
#1        B      76.0   77.0  127.0
#2        C      28.0   69.0   16.0
#3        D      28.0   28.0   31.0
#4        E      19.0   20.0   85.0
#5        F      84.0  193.0   70.0
#total  NaN     319.0  400.0  398.0

使用loc替换数据帧:

df.loc['total'] = df.select_dtypes(pd.np.number).sum()
df
#         X  MyColumn      Y      Z
#0        A      84.0   13.0   69.0
#1        B      76.0   77.0  127.0
#2        C      28.0   69.0   16.0
#3        D      28.0   28.0   31.0
#4        E      19.0   20.0   85.0
#5        F      84.0  193.0   70.0
#total  NaN     638.0  800.0  796.0

类似于获取一个数据帧的长度len(df),以下方法适用于pandas和blaze:

Total = sum(df['MyColumn'])

或者

Total = sum(df.MyColumn)
print Total

作为其他选择,您可以像下面这样做

Group   Valuation   amount
0   BKB Tube    156
1   BKB Tube    143
2   BKB Tube    67
3   BAC Tube    176
4   BAC Tube    39
5   JDK Tube    75
6   JDK Tube    35
7   JDK Tube    155
8   ETH Tube    38
9   ETH Tube    56

以下脚本,您可以使用以上数据

import pandas as pd
data = pd.read_csv("daata1.csv")
bytreatment = data.groupby('Group')
bytreatment['amount'].sum()

一列的和有两种方法

数据集= pd.read_csv("data.csv")

1: sum (dataset.Column_name)

2:数据集(Column_Name”).sum ()

如果有任何问题,请纠正我。