Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing additional arguments to python pandas DataFrame apply

Tags:

python

pandas

I have a DataFrame df and I tried to iterate each row to map values of two columns to new values, but I have problems passing in the dictionary containing the map to df.apply

df.apply(lambda row: (map_dict[row['colA']], map_dict[row['colB']]), axis=1, args=(map_dict,), map_dict=map_dict)

I got the error message

File "<console>", line 1
SyntaxError: Generator expression must be parenthesized if not sole argument

I tried to read the official doc , but I struggle to understand its explanation on args parameter http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.apply.html

I have also tried passing in map_dict as either keyword or positional argument, but I still get the same error

like image 669
Michael Avatar asked Sep 07 '15 11:09

Michael


1 Answers

The args argument given to apply function is passed func argument (lambda function given). You are getting this error since two arguments are given but lambda function only accepts one argument.

Hope this example helps

import pandas as pd
df = pd.DataFrame({'a':range(100,110), 'b':range(200, 210)})

def modulo(x, n=5):
    return x%n

some_dict = {0: 'a', 1:'b', 2:'c', 3:'d', 4:'e'}
print(df.apply(lambda row,n, map_dict: (map_dict[modulo(row['a'],n)], map_dict[modulo(row['b'],n)]), axis=1, args=(5, some_dict)))
like image 122
shanmuga Avatar answered Sep 28 '22 23:09

shanmuga