Iterations over items of pandas dataframe

56 views Asked by At

I have read many times that iterations should be avoided in dataframes so I have been trying the "better ways", such as applying functions, but I get stuck with the following error:

The truth value of a Series is ambiguous

I need to run iterative calculations across various row items and get updated values. Here is an simplified example, but the real case has a lot of math in it hence why functions are preferred:

df = pd.DataFrame({'A':[10,20,30,40], 'B':[4,3,2,1]})

def match_col(A,B):
    while A != B:
        B = B + 1

df.apply(lambda x: match_col(df['A'],df['B']),axis=1)

Basically, I need for each row to use a number of items, run iterative calcs, and output new/updated items. Where am I getting the logic wrong?

2

There are 2 answers

1
mechanical_meat On BEST ANSWER

Instead do:

df.apply(lambda x: match_col(x['A'],x['B']),axis=1)

Because you're applying the function over each row, the row's values are what need to be passed to match_col and not entire series e.g. df['A'].

You also need to return something from your function:

def match_col(A,B):
    while A != B:
        B = B + 1
    return B

Then you'll get this result:

In [10]: df.apply(lambda x: match_col(x['A'],x['B']),axis=1)                    
Out[10]: 
0    10
1    20
2    30
3    40
dtype: int64
0
The Guy On

I did some changes in apply function

import numpy as np
import pandas as pd

df = pd.DataFrame({'A':[10,20,30,40], 'B':[1,3,2,1]})

def match_col(col):
    while col.A != col.B:
        col.B = col.B + 1
        return col.B


df.apply(match_col,axis=1)

Output

0    2
1    4
2    3
3    2
dtype: int64