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

后端 未结 1 1937
北荒
北荒 2020-12-10 08:29

I would like to apply a function with argument to a pandas series: I have found two different solution of SO:

python pandas: apply a function with arguments to a ser

相关标签:
1条回答
  • 2020-12-10 09:08

    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
    
    0 讨论(0)
提交回复
热议问题