简体   繁体   中英

Apply function to pandas Series with argument (which varies for every element)

I have a pandas Series and a function that I want to apply to each element of the Series. The function have an additional argument too. So far so good: for example

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

What about if the argument varies by itself running over a given list? I had to face this problem in my code and I have found a straightforward solution but it is quite specific and (even worse) do not use the apply method.

Here is a toy model code:

a=pd.DataFrame({'x'=[1,2]})
t=[10,20]

I want to multiply elements in a['x'] by elements in t. Here the function is quite simple and len(t) matches with len(a['x'].index) so I could just do:

a['t']=t
a['x*t']=a['x']*a['t']

But what about if the function is more elaborate or the two lengths do not match?

What I would like is a command line like:

a['x'].apply(lambda x,y: x*y, arg=t)

The point is that this specific line exits with an error because the arg variable in that case will accept only a tuple of len=1. I do not see any 'place' to put the various element of t.

What you're looking for is similar to what R calls "recycling", where operations on arrays of unequal length loops through the smaller array over and over as many times as needed to match the length of the longer array.

I'm not aware of any simple, built-in way to do this with numpy or pandas. What you can do is use np.tile to repeat your smaller array. Something like:

a.x*np.tile(t, len(a)/len(t))

This will only work if the longer array's length is a simple multiple of the shorter one's.

The behavior you want is somewhat unusual. Depending on what you're doing, there may be a better way to handle it. Relying on the values to match up in the desired way just by repetition is a little fragile. If you have some way to match up the values in each array that you want to multiply, you could use the .map method of Series to select the right "other value" to multiply each element of your Series with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM