pandas 一步一步依次填写

pandas fillna sequentially step by step

我有如下数据框

Re_MC,Fi_MC,Fin_id,Res_id,     
1,2,3,4
,7,6,11
11,,31,32
,,35,38

df1 = pd.read_clipboard(sep=',')

我想fillna基于两个步骤

a) 首先,只比较 Re_MCFi_MC。如果其中任一列中缺少某个值,请从另一列中复制它。

b) 尽管执行了步骤 a,如果 Re_MCFi_MC 仍然存在 NA,则从 Fin_id 复制 Fi_MC 和 [=22= 的值] 对于 Re_MC.

所以,我尝试了以下两种方法

方法 1 - 这有效但无效 efficient/elegant

df1['Re_MC'] = df1['Re_MC'].fillna(df1['Fi_MC'])
df1['Fi_MC'] = df1['Fi_MC'].fillna(df1['Re_MC'])
df1['Re_MC'] = df1['Re_MC'].fillna(df1['Res_id'])
df1['Fi_MC'] = df1['Fi_MC'].fillna(df1['Fin_id'])

方法 2 - 这不起作用并提供不正确的输出

df1['Re_MC'] = df1['Re_MC'].fillna(df1['Fi_MC']).fillna(df1['Res_id'])
df1['Fi_MC'] = df1['Fi_MC'].fillna(df1['Re_MC']).fillna(df1['Fin_id'])

有没有其他有效的方法来按顺序填写na?意思是,我们先做 step a 然后根据 step a 的结果,我们做 step b

我希望我的输出如下所示

更新代码

df_new = (df_new 
 .fillna({'Re MC': df_new['Re Cust'],'Re MC': df_new['Re Cust_System']})
 .fillna({'Fi MC' : df_new['Fi.Fi Customer'],'Final MC':df_new['Re.Fi Customer']})
 .fillna({'Fi MC' : df_new['Re MC']})
 .fillna({'Class Fi MC':df_new['Re MC']})
)

您可以在fillna中使用字典:

(df1
 .fillna({'Re_MC': df1['Fi_MC'], 'Fi_MC': df1['Re_MC']})
 .fillna({'Re_MC': df1['Res_id'], 'Fi_MC': df1['Fin_id']})
)

输出:

   Re_MC  Fi_MC  Fin_id  Res_id
0    1.0    2.0       3       4
1    7.0    7.0       6      11
2   11.0   11.0      31      32
3   38.0   35.0      35      38