pandas DataFrame中的新列以其他列的值为条件

时间:2017-01-26 14:12:50

标签: python pandas

我有以下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']})

我想创建一个新列:

df['total energy consumption'] 

将energy_per_capita和pop相乘。 我希望它可以在pop_2015可用时使用pop_2014如果pop_2015 == NaN

感谢

1 个答案:

答案 0 :(得分:4)

请务必阅读10 Minutes to pandas。对于这种情况,我们使用pandas.DataFrame.fillna方法

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

输出

  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