Kgabo
Kgabo

Reputation: 1

Dataframe column status

On the column for status

enter image description here

I want set status as 1 if diff is less than 0 and 1 if is more than 1.

Upvotes: 0

Views: 496

Answers (2)

Marco Caldera
Marco Caldera

Reputation: 505

You can use np.where or, if you prefer, you can simply apply a lambda function like this:

df['status'] = df['diff'].apply(lambda val: 1 if val < 0 or val > 1 else np.nan)

As default value you can use np.nan or any other value that you like.

Upvotes: 1

Shubham Sharma
Shubham Sharma

Reputation: 71687

You can use np.where to choose 1 or '' depending on the condition.

Use this:

import numpy as np

df_small["status"] = np.where((df_small["diff"] < 0) | (df_small["diff"] > 1), 1, '')

Upvotes: 1

Related Questions