Pandas DataFrame - assign 1,0 values based on other column

前端 未结 2 1447
感动是毒
感动是毒 2021-01-21 04:59

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

2条回答
  •  余生分开走
    2021-01-21 05:44

    @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)
    

提交回复
热议问题