Apply a function to a column in a pandas dataframe that takes two arguments

Say I have a specific mapping:

mapping = { 'cat': 'purrfect', 'dog': 'too much work', 'fish': 'meh' } 

and a dataframe :

  animal name description 0 cat sparkles NaN 1 dog rufus NaN 2 fish mr. blub NaN 

I would like to programmatically populate the description column using the animal and mapping dict column as input:

 def describe_pet(animal,mapping): return mapping[animal] 

When I try to use the pandas apply() function:

 df['description'].apply(describe_pet,args=(df['animal'],mapping)) 

I get the following error:

 TypeError: describe_pet() takes exactly 2 arguments (3 given) 

It seems that using apply() trivially passes one argument to the function. How to do this with two arguments?

+4
source share
2 answers

The suggested answer solves your specific problem, but for a more general case:

The args parameter is for parameters in addition to the columns:

args: tuple Positional arguments to jump to a function in addition to array / series

pandas.DataFrame.apply

+2
source

You can do this using the map method without writing a function or using apply :

 df['description'] = df.animal.map(mapping) 
+5
source

Source: https://habr.com/ru/post/1238208/


All Articles