Python Pandas,应用函数 [英] Python Pandas, apply function
问题描述
我正在尝试使用 apply 来避免函数中的 iterrows()
迭代器:
I am trying to use apply to avoid an iterrows()
iterator in a function:
但是,pandas 方法的文档很差,我找不到有关如何使用它的示例,除了文档中的 .apply(sq.rt)
... 没有示例如何使用参数等...
However that pandas method is poorly documented and I can't find example on how to use it, except for the lame .apply(sq.rt)
in the documentation... No example on how to use arguments etc...
无论如何,这是一个关于我尝试做的事情的玩具示例.
Anyway, here a toy example on what I try to do.
根据我的理解,apply
实际上与 iterrows()
的作用相同,即迭代(如果轴 = 0,则遍历行).在每次迭代中,函数的输入 x
应该是迭代过的行.然而,我不断收到的错误消息反驳了这个假设......
In my understanding apply
will actually do the same as iterrows()
, ie, iterate (over the rows if axis=0). On each iteration the input x
of the function should be the row iterated over. However the error messages I keep receiving sort of disprove that assumption...
grid = np.random.rand(5,2)
df = pd.DataFrame(grid)
def multiply(x):
x[3]=x[0]*x[1]
df = df.apply(multiply, axis=0)
上面的例子返回一个空的 df.谁能解释一下我的误解?
The example above returns an empty df. Can anyone shed some light on my misunderstanding?
推荐答案
import pandas as pd
import numpy as np
grid = np.random.rand(5,2)
df = pd.DataFrame(grid)
def multiply(x):
return x[0]*x[1]
df['multiply'] = df.apply(multiply, axis = 1)
print(df)
结果:
0 1 multiply
0 0.550750 0.713054 0.392715
1 0.061949 0.661614 0.040987
2 0.472134 0.783479 0.369907
3 0.827371 0.277591 0.229670
4 0.961102 0.137510 0.132162
<小时>
说明:
您正在应用
的函数需要返回一个值.您还将它应用于每一行,而不是列.轴
您传递的参数在这方面不正确.
The function you are apply
ing, needs to return a value. You are also applying this to each row, not column. The axis
parameter you passed was incorrect in this regard.
最后,请注意我将其设置为等于函数外的 'multiply'
列.您可以轻松地将其更改为 df[3] = ...
,就像您拥有的那样并获得这样的数据框:
Finally, notice that I am setting this equal to the 'multiply'
column outside of my function. You can easily change this to be df[3] = ...
like you have and get a dataframe like this:
0 1 3
0 0.550750 0.713054 0.392715
1 0.061949 0.661614 0.040987
2 0.472134 0.783479 0.369907
3 0.827371 0.277591 0.229670
4 0.961102 0.137510 0.132162
这篇关于Python Pandas,应用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!