import pandas as pd
df1 = pd.DataFrame([['update me', 'leave', 'take the other nan']], index=[0], columns=['A', 'B', 'C'])
df2 = pd.DataFrame([['update with me', pd.np.nan, 'stay out']], index=[0], columns=['A', 'C', 'D'])
# want something like: df1.update_using_nans_please(df2) # to return:
# pd.DataFrame([['update with me', 'leave', pd.np.nan]], columns=['A', 'B', 'C'])
df1.update(df2.fillna('nan'))
df1.replace('nan', pd.np.nan) # Any way to do it without this hack?
解决办法:
作为替代方案,我们可以使用一点索引魔法pd.concat。然而,由于上述原因,这不执行就地修改。
a, b = df1.columns, df2.columns
pd.concat([df1[a.difference(b)], df2[a.intersection(b)]], axis=1)
B A C
0 leave update with me NaN
要保留原始订单,
pd.concat([df1[a.difference(b)], df2[a.intersection(b)]], axis=1).reindex_like(df1)
A B C
0 update with me leave NaN








暂无数据