python pandas: apply a function with arguments to a series. Update

二次信任 提交于 2019-11-28 11:37:57

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!