Reputation: 13
This is my dataframe. I want to create a new column named mpg_high
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
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
Reputation: 195418
You can use np.where
:
df['mpg_high'] = np.where(df.mpg > np.mean(df.mpg), 1, 0)
Upvotes: 1