I\'ve got a dataframe containing country names & their percentage of energy output. I need to add a new column that assigns a 1 or 0, based on whether the country\'s energy
@Vaishali explains why pd.DataFrame.where
didn't work as you expected and suggested you use np.where
instead, which is very good advice.
I'll offer up that you could have simply converted your boolean result to integers.
Setup
df = pd.DataFrame({
'name':['china', 'america', 'canada'],
'output': [33.2, 15.0, 5.0]
})
Option 1
df['newcol'] = (df['output'] > df['output'].median()).astype(int)
Option 2
Or faster yet by using the underlying numpy arrays
o = df['output'].values
df['newcol'] = (o > np.median(o)).astype(int)