Tote
Tote

Reputation: 13

How to create a New Column in Pandas on if else condition in Python

This is my dataframe. I want to create a new column named mpg_high

enter image description here

The condition of the column is: mpg_high = 1 if mpg > average mpg (this is the mean of mpg where I placed np.mean(df.mpg) else = 0.

I tried various methods but kept getting syntax errors. How can I do this?

Thanks in advance

Upvotes: 1

Views: 154

Answers (2)

Rob Raymond
Rob Raymond

Reputation: 31146

It can be reduced to straight binary comparison and conversion to int

df = pd.DataFrame({"mgp":np.random.randint(15,35, 50)})

df["mgp_high"] = df["mgp"].gt(df["mgp"].mean()).astype(int)

Upvotes: 1

Andrej Kesely
Andrej Kesely

Reputation: 195418

You can use np.where:

df['mpg_high'] = np.where(df.mpg > np.mean(df.mpg), 1, 0)

Upvotes: 1

Related Questions