vendredi 10 août 2018

lamba function with if else

I have a dataframe that looks like:

A   B   C   D   SUM 
2   5   -4  12  15

I try and run:

df.apply((lambda x: x / x.sum() if x/x.sum() >= 0 else None), axis=1).fillna(0)

to get, if cell is same at total then calculate x/total:

A         B     C   D
2/15    5/15    0   12/15

I get:

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

How can i improve the code.

Aucun commentaire:

Enregistrer un commentaire