Pierre
Pierre

Reputation: 53

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

Upvotes: 0

Views: 315

Answers (1)

Shijo
Shijo

Reputation: 9721

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

Upvotes: 4

Related Questions