在合并/左连接期间替换数据框中的 NaN

Replace NaN in dataframe during merging / left join

我正在将两个数据帧合并在一起作为左连接。但是,如果特定列中的值是空白或 NaN,我想从“正确的”数据框中替换该值(并且仅在这种情况下。否则,我想忽略 'Cost' 中的数据df2)

df1 = pd.DataFrame({
         'ID':[1,2,3,4,5,6],
         'Version':[1,1,2,2,1,2],
         'Cost':[17,np.nan,24,21,'',8]})

df2 = pd.DataFrame({
         'ID':[1,2,3,4,5,6,7,8,9],
         'Color':["Red","Orange","Green","Blue","Indigo", "Violet","Black","White","Gold"],
         'UnUsedData': ['foo','bar','foo','bar','foo','bar','foo','bar','foo'],
         'Cost':[17,34,54,28,22,8,43,23,12]})

合并语句为:

df_new = pd.merge(df1, df2[['ID','Color']], on ='ID', how ='left')

产生:

   ID  Version Cost   Color
0   1        1   17     Red
1   2        1   NaN  Orange
2   3        2   24   Green
3   4        2   21    Blue
4   5        1       Indigo
5   6        2    8  Violet

但我希望输出看起来像:[索引行 #s 1 和 4 中的成本列值发生变化]

   ID  Version Cost   Color
0   1        1   17   Red
1   2        1   34   Orange
2   3        2   24   Green
3   4        2   21   Blue
4   5        1   22   Indigo
5   6        2    8   Violet

我可以遍历 df_new 的成本列的各个值,然后在 df2 中查找每个空白或 NaN 的值,但似乎会有更多 elegant/simpler 方法。也许以某种方式使用 fillna()?我看到的例子似乎是用一个常量值代替 NaN,而不是根据项目变化的值。

您可以使用combine_first获取第一个非na信息:

# merge
dfx = pd.merge(df1, df2[['ID','Color','Cost']], on ='ID', how ='left')

# replace empty space with NAN
dfx = dfx.replace("", np.nan)

# coalesce cost column to get first non NA value
dfx['Cost'] = dfx['Cost_x'].combine_first(dfx['Cost_y']).astype(int)

# remove the cols
dfx = dfx.drop(['Cost_x', 'Cost_y'], 1)
print(dfx)

   ID  Version   Color  Cost
0   1        1     Red    17
1   2        1  Orange    34
2   3        2   Green    24
3   4        2    Blue    21
4   5        1  Indigo    22
5   6        2  Violet     8