Birish
Birish

Reputation: 5832

loop over all rows in a dataframe and generate new column based on comparing other columns

I have the following dataframe:

ID name value mean std upper lower
894.68 154.00 2.33 203.16 189.18
1045.28 196.17 4.50 204.00 186

For each row, I'm trying to create a new column by comparing the value with upper and lower as follow:

df['new_col'] = df[df['mean'].notnull()].apply(lambda x: False if x['value']>x['upper'] or x['value']<x['lower'] else True)

It gives me an error which is not ver clear to me: KeyError: 'value'. I guess it can't find x['value'], right? How do I fix it?

Upvotes: 0

Views: 38

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24322

Try putting your condition directly that will give you the boolean Series directly:

df['new_col'] = (~((df['value']>df['upper']) | (df['value']<df['lower'])) & df['mean'].notnull())

OR

via apply() but it will be slow because it is loop under the hood so pass axis=1:

df['new_col'] = df[df['mean'].notnull()].apply(lambda x: False if x['value']>x['upper'] or x['value']<x['lower'] else True,axis=1)

output of df:

     ID     name    value   mean    std     upper   lower   new_col
0   NaN     NaN     894.68  154.00  2.33    203.16  189.18  False
1   NaN     NaN     1045.28 196.17  4.50    204.00  186.00  False

Upvotes: 1

Related Questions