vendredi 12 février 2021

Python/ Pandas If statement inside a function explained

I have the following example and I cannot understand why it doesn't work.

import pandas as pd

d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)

def balh(a, b):
    z = a + b
    if z.any() > 1:
        return z + 1
    else:
        return z

df['col3'] = balh(df.col1, df.col2)

Output:

enter image description here

My expected output would be see 5 and 7 not 4 and 6 in col3, since 4 and 6 are grater than 1 and my intention is to add 1 if a + b are grater than 1

Aucun commentaire:

Enregistrer un commentaire