且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

python pandas:将带有参数的函数应用于系列.更新

更新时间:2022-05-03 10:01:22

TypeError表示您将错误的类型传递给lambda函数x + y.期望args是一个序列,但是得到了int.您可能以为(100)是一个元组(一个序列),但是在python中,逗号是一个元组:

The TypeError is saying that you passed the wrong type to the lambda function x + y. It's expecting the args to be a sequence, but it got an int. You may have thought that (100) was a tuple (a sequence), but in python it's the comma that makes a tuple:

In [10]: type((100))
Out[10]: int

In [11]: type((100,))
Out[11]: tuple

因此将您的最后一行更改为

So change your last line to

In [12]: a['x'].apply(lambda x, y: x + y, args=(100,))
Out[12]: 
0    101
1    102
Name: x, dtype: int64