简体   繁体   English

为什么sum(DF)与DF.sum()的行为不同?

[英]Why does sum(DF) behave differently from DF.sum()?

In pandas 0.14, sum(DF) returns the sum of the headers, rather than the sum of the entries. 在pandas 0.14中, sum(DF)返回标题的总和,而不是条目的总和。 Thus, one gets, for example: 因此,例如:

>df = pandas.DataFrame([1,2,3])
>print(df)
   0
0  1
1  2
2  3
>sum(df)
0

Is there a logical reason, a use case for this? 有逻辑原因,这是一个用例吗?

buildin function sum() work as: buildin函数sum()工作原理如下:

sum(list(iter(df)))

and iter(df) get an iterator of column names. iter(df)获取列名称的迭代器。

you should not use buildin sum() for DataFrame. 你不应该为DataFrame使用buildin sum()

Or use numpy.sum : 或者使用numpy.sum

In [43]:

df = pd.DataFrame([1,2,3])
print 'numpy:', np.sum(df)
print '.sum() method:',df.sum()
print 'buid-in:', sum(df)
numpy: 0    6
dtype: int64
.sum() method: 0    6
dtype: int64
buid-in: 0

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

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