我有一个已连接的数据框,它具有两个相同的列,但具有两个不同的名称。
它看起来像这样:
Fuel Category Gen MW Gen MWh Time Stamp Time Zone
Dual Fuel 2809.0 nan 12/09/2015 00:05 EST
Hydro 2224.0 nan 12/09/2015 00:05 EST
Natural Gas nan 150.00 12/09/2016 00:05 EST
Nuclear 4787.0 nan 12/09/2015 00:05 EST
Other Fossil nan 20.0 12/09/2016 00:05 EST
问题在于,在不同的时间段,一个标头被称为Gen MWh
,另一个标头Gen MW
。我只想添加一个新列,并将其填充到df['Gen New MW']
时df[Gen MW]
= df['Gen MWh']='nan'
和df[Gen MWh]
时= df['Gen MW']='nan'
的地方。
我尝试过:
for i in range(0, len(df)):
if df['Gen MW'].iloc[i] == 'nan':
df['Generation (MW)'].iloc[i] == df['Gen MWh'].iloc[i]
elif df['Gen MWh'].iloc[i] == 'nan':
df['Generation (MW)'].iloc[i] == df['Gen MW'].iloc[i]
但这会反复进行,不会产生任何结果。
有没有一种方法可以轻松做到这一点?
答案 0 :(得分:2)
按照@piRsquared的建议使用Combine_first或fillna,
model1 <- "
E =~ score + raven + aac
R =~ raven
C =~ aac
"
选项2:
df['Gen New MW'] = df['Gen MW'].combine_first(df['Gen MWh'])
df['Gen New MW']
0 2809.0
1 2224.0
2 150.0
3 4787.0
4 20.0