简体   繁体   中英

New column in pandas DataFrame conditional on value of other columns

I have the following pandas DataFrame:

df = pd.DataFrame({'country' : ['US','FR','DE','SP'], 
    'energy_per_capita': [10,8,9,7], 
    'pop_2014' : [300,70,80,60],
    'pop_2015': [305,72,80,'NaN']})

I'd like to create a new column:

df['total energy consumption'] 

which multiplies energy_per_capita and pop. I'd like it to take pop_2015 when available and pop_2014 if pop_2015 == NaN

thanks

make sure you read 10 Minutes to pandas . For this case we are using pandas.DataFrame.fillna method

df = pd.DataFrame({'country' : ['US','FR','DE','SP'], 
    'energy_per_capita': [10,8,9,7], 
    'pop_2014' : [300,70,80,60],
    'pop_2015': [305,72,80,np.nan]})

df['total energy consumption']= df['energy_per_capita'] *df['pop_2015'].fillna(df['pop_2014'])
print df

output

  country  energy_per_capita  pop_2014  pop_2015  total energy consumption
0      US                 10       300     305.0                    3050.0
1      FR                  8        70      72.0                     576.0
2      DE                  9        80      80.0                     720.0
3      SP                  7        60       NaN                     420.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM