python pandas: apply a function with arguments to a series
Newer versions of pandas do allow you to pass extra arguments (see the new documentation). So now you can do:
my_series.apply(your_function, args=(2,3,4), extra_kw=1)
The positional arguments are added after the element of the series.
For older version of pandas:
The documentation explains this clearly. The apply method accepts a python function which should have a single parameter. If you want to pass more parameters you should use functools.partial
as suggested by Joel Cornett in his comment.
An example:
>>> import functools
>>> import operator
>>> add_3 = functools.partial(operator.add,3)
>>> add_3(2)
5
>>> add_3(7)
10
You can also pass keyword arguments using partial
.
Another way would be to create a lambda:
my_series.apply((lambda x: your_func(a,b,c,d,...,x)))
But I think using partial
is better.
pandas apply function with arguments
Pass the arguments as kwargs to apply
:
df['events'] = df['data'].apply(count_ones, total_bits=60, group_size=12)
Pandas apply function treating input as a series rather than a single cell in the passed series
You have to pass the row, not the entire column:
bank_exp['Description'] = bank_exp.apply(lambda row : preprocess_cell(row['Description'], stopwords_all), axis = 1)
Or run apply on the Series:
bank_exp['Description'] = bank_exp['Description'].apply(preprocess_cell, args=(stopwords_all,))
python pandas- apply function with two arguments to columns
Why not just do this?
df['NewCol'] = df.apply(lambda x: segmentMatch(x['TimeCol'], x['ResponseCol']),
axis=1)
Rather than trying to pass the column as an argument as in your example, we now simply pass the appropriate entries in each row as argument, and store the result in 'NewCol'
.
Passing a function with multiple arguments to DataFrame.apply
It's just the way you think it would be, apply
accepts args
and kwargs
and passes them directly to some_func
.
df.apply(some_func, var1='DOG', axis=1)
Or,
df.apply(some_func, args=('DOG', ), axis=1)
0 foo-x-DOG
1 bar-y-DOG
dtype: object
Pass arguments to function while using apply to pandas series
IIUC you need pass like argument dropna=False
:
print (df.apply(pd.Series.value_counts, dropna=False))
a b c
1.0 1.0 NaN NaN
2.0 2.0 1.0 NaN
3.0 NaN 2.0 NaN
4.0 NaN 1.0 2.0
6.0 NaN NaN 1.0
NaN 1.0 NaN 1.0
Or lambda function:
print (df.apply(lambda x: x.value_counts(dropna=False)))
a b c
1.0 1.0 NaN NaN
2.0 2.0 1.0 NaN
3.0 NaN 2.0 NaN
4.0 NaN 1.0 2.0
6.0 NaN NaN 1.0
NaN 1.0 NaN 1.0
python pandas: apply a function with arguments to a series. Update
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
Applying function with multiple arguments to create a new pandas column
Alternatively, you can use numpy underlying function:
>>> import numpy as np
>>> df = pd.DataFrame({"A": [10,20,30], "B": [20, 30, 10]})
>>> df['new_column'] = np.multiply(df['A'], df['B'])
>>> df
A B new_column
0 10 20 200
1 20 30 600
2 30 10 300
or vectorize arbitrary function in general case:
>>> def fx(x, y):
... return x*y
...
>>> df['new_column'] = np.vectorize(fx)(df['A'], df['B'])
>>> df
A B new_column
0 10 20 200
1 20 30 600
2 30 10 300
Pandas .apply() function with multiple args
You can use np.where
and reduce this to 1 line.
s = rocksfile['Song_Clean']
+ ' was released by '
+ rocksfile['Artist_Clean']
+ pd.Series(np.where(rocksfile['Release_Year'] < 1970, 'before', 'after'))
+ ' 1970'
rocksfile['new'] = s
Related Topics
Importerror: Libcblas.So.3: Cannot Open Shared Object File: No Such File or Directory
Creating Same Random Number Sequence in Python, Numpy and R
Computing Cross-Correlation Function
Quick and Easy File Dialog in Python
Create a .CSV File with Values from a Python List
Pandas: Rolling Mean by Time Interval
How to Write to a CSV Line by Line
Python and Regular Expression with Unicode
How to Do Parallel Programming in Python
How to Make an Immutable Object in Python
Python Requests. 403 Forbidden
Permissionerror: [Errno 13] in Python
Selenium Give File Name When Downloading
How to Run an External Command Asynchronously from Python
Remove All Special Characters, Punctuation and Spaces from String