Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple arguments to the apply function

I have a method called counting that takes 2 arguments. I need to call this method using the apply() method. However when I am passing the two parameters to the apply method it is giving the following error:

TypeError: counting() takes exactly 2 arguments (1 given)

I have seen the following thread python pandas: apply a function with arguments to a series. Update and I do not want to use functool.partial as I do not want to import additional classes to be able to pass parameters.

def counting(dic, strWord):
    if strWord in dic:
        return dic[strWord]
    else:
        return 0

DF['new_column'] = DF['dic_column'].apply(counting, 'word')

If I give a single parameter, it works:

def awesome_count(dic):
    if strWord in dic:
       return dic[strWord]
    else:
       return 0

DF['new_column'] = DF['dic_column'].apply(counting)
like image 398
Bonson Avatar asked Oct 19 '15 00:10

Bonson


People also ask

How do you pass multiple arguments in Java?

Parameters and Arguments You can add as many parameters as you want, just separate them with a comma.

How do you add multiple arguments to a list in Python?

extend to extend the list by multiple values from any kind of iterable, being it another list or any other thing that provides a sequence of values. So you can use list. append() to append a single value, and list. extend() to append multiple values.

How do you pass an argument to a function in Python?

Information can be passed into functions as arguments. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.


2 Answers

You could just use a lambda:

DF['new_column'] = DF['dic_column'].apply(lambda dic: counting(dic, 'word'))

On the other hand, there's absolutely nothing wrong with using partial here:

from functools import partial
count_word = partial(counting, strWord='word')
DF['new_column'] = DF['dic_column'].apply(count_word)

As @EdChum mentions, if your counting method is actually just looking up a word or defaulting it to zero, you can just use the handy dict.get method instead of writing one yourself:

DF['new_column'] = DF['dic_column'].apply(lambda dic: dic.get('word', 0))

And a non-lambda way to do the above, via the operator module:

from operator import methodcaller
count_word = methodcaller(get, 'word', 0)
DF['new_column'] = DF['dic_column'].apply(count_word)
like image 64
tzaman Avatar answered Sep 29 '22 07:09

tzaman


The accepted answer is totally perfect. Taught me some interesting things about Python. But just for fun, here's more precisely what we're looking for:

selected_words =  ['awesome', 'great', 'fantastic', 'amazing', 'love', 'horrible', 'bad', 'terrible', 'awful', 'wow', 'hate']
for this_word in selected_words:
    products[this_word] = products['word_count'].apply(lambda dic: dic.get(this_word, 0))

Thanks for posting the question!

like image 20
L0j1k Avatar answered Sep 29 '22 07:09

L0j1k