我正在使用 pandas 13.0,尝试使用 apply() 和函数名 foo() 创建一个新的列。
我的数据框如下:
df = pandas.DataFrame({
'a':[ 0.0, 0.1, 0.2, 0.3],
'b':[10.0, 20.0, 30.0, 40.0],
'c':[ 1.0, 2.0, 3.0, 4.0]
})
df.set_index(df['a'], inplace=True)
我的数据框如下:
in: print df
out:
a b c
a
0.0 0.0 10.0 1.0
0.1 0.1 20.0 2.0
0.2 0.2 30.0 3.0
0.3 0.3 40.0 4.0
我的函数如下所示:
def foo(arg1, arg2):
return arg1*arg2
现在我想使用foo()创建一个名为“d”的列;
df['d'] = df.apply(foo(df['b'], df['c']), axis=1)
但是我遇到了以下错误:
TypeError: ("'Series' object is not callable", u'occurred at index 0.0')
如何在由浮点数组成的索引中使用pandas.apply()和foo()方法?
谢谢。
apply
。对于基本的加/减/除和乘法运算,内置的运算符支持速度要快得多。 - EdChum