Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas, apply with args which are dataframe row entries

I have a pandas dataframe 'df' with two columns 'A' and 'B', I have a function with two arguments

def myfunction(B, A):
    # do something here to get the result
    return result

and I would like to apply it row-by-row to df using the 'apply' function

df['C'] = df['B'].apply(myfunction, args=(df['A'],))

but I get the error

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

whats happening here, it seems it takes df['A'] as the whole series! not just the row entry from that series as required.

like image 764
Runner Bean Avatar asked Oct 02 '16 06:10

Runner Bean


People also ask

How do I apply a function to all rows of a data frame?

Use apply() function when you wanted to update every row in pandas DataFrame by calling a custom function. In order to apply a function to every row, you should use axis=1 param to apply(). By applying a function to each row, we can create a new column by using the values from the row, updating the row e.t.c.

How do I apply a function to a DataFrame in pandas?

DataFrame - apply() function The apply() function is used to apply a function along an axis of the DataFrame. Objects passed to the function are Series objects whose index is either the DataFrame's index (axis=0) or the DataFrame's columns (axis=1).

Which method takes row column as argument in Python?

Applying a function to all rows in a Pandas DataFrame is one of the most common operations during data wrangling. Pandas DataFrame apply function is the most obvious choice for doing it. It takes a function as an argument and applies it along an axis of the DataFrame.

What is passed to the apply function?

You can pass any number of arguments to the function that apply is calling through either unnamed arguments, passed as a tuple to the args parameter, or through other keyword arguments internally captured as a dictionary by the kwds parameter.


1 Answers

I think you need:

import pandas as pd
df = pd.DataFrame({'A':[1,2,3],
                   'B':[4,5,6]})

print (df)
   A  B
0  1  4
1  2  5
2  3  6

def myfunction(B, A):
    #some staff  
    result = B + A 
    # do something here to get the result
    return result

df['C'] = df.apply(lambda x: myfunction(x.B, x.A), axis=1)
print (df)
   A  B  C
0  1  4  5
1  2  5  7
2  3  6  9

Or:

def myfunction(x):

    result = x.B + x.A
    # do something here to get the result
    return result

df['C'] = df.apply(myfunction, axis=1)
print (df)
   A  B  C
0  1  4  5
1  2  5  7
2  3  6  9
like image 95
jezrael Avatar answered Oct 06 '22 00:10

jezrael