问题描述
我正在学习 Pandas 和 Numpy,目前正在学习 教程的这一部分.我完全是 Python 新手,所以这可能是一个基本的初学者问题.
I'm learning Pandas and Numpy, currently going through this section of the tutorial. I'm new to Python altogether, so this is probably a basic beginner's question.
给定这个数据框:
df = pd.DataFrame(np.random.randn(4, 3), columns=['A', 'B', 'C'],
index=pd.date_range('1/1/2000', periods=4))
df.iloc[3:7] = np.nan
我无法解释 df.agg
:
I can't explain the difference between the following results of df.agg
:
呼叫 1:
df.agg(sum)
#Result:
A NaN
B NaN
C NaN
dtype: float64
呼叫 2:
df.agg('sum')
#Result:
A -1.776752
B -2.070156
C -0.124162
dtype: float64
df.agg('sum')
的结果与 df.agg(np.sum)
或 df.sum()
.我希望 df.agg('sum')
产生与 df.agg(sum)
相同的结果.
The result of df.agg('sum')
is the same as that of df.agg(np.sum)
or df.sum()
. I expected df.agg('sum')
to produce the same result as df.agg(sum)
.
Pandas 是否有特殊的逻辑来解析这些函数,以便它更喜欢 np.sum
(或运行 df.sum
)而不是内置的 求和
?
Does Pandas have special logic to resolve these functions such that it would prefer np.sum
(or run df.sum
) instead of the built-in sum
?
推荐答案
在您链接到的文档中,它说:
In the documentation you linked to, it says:
您也可以将命名方法作为字符串传递.
因此字符串被解析为 DataFrame 上的方法名称(或 Series,如果您在 Series 上调用 agg
).
So strings are resolved as method names on the DataFrame (or Series, if you call agg
on a Series).
这篇关于Pandas 如何解析df.agg中name指定的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!