讓我們假設'steve'
有,我們要在df1
保留了一句話和'jack'
了我們想在df2
保存的話。我們可以設置每個數據幀的指數來['id', 'name']
和使用pd.Series.combine_first
設置
df1 = pd.DataFrame(dict(
id=[12, 34, 56, 78, 90, 13],
name='james steve jack ted eric bob'.split(),
remark='',
))
df1.at[1, 'remark'] = 'meh'
df2 = pd.DataFrame(dict(
id=[12, 34, 56, 78, 13],
name='james steve jack ted bob'.split(),
remark='',
))
df2.at[2, 'remark'] = 'smart'
解決方案
s1 = df1.set_index(['id', 'name']).remark
s2 = df2.set_index(['id', 'name']).remark
s1.mask(s1.eq('')).combine_first(s2.mask(s2.eq(''))).fillna('').reset_index()
id name remark
0 12 james
1 13 bob
2 34 steve meh
3 56 jack smart
4 78 ted
5 90 eric
然而,supposin它完全如同OP介紹的那樣!
設置
df1 = pd.DataFrame(dict(
id=[12, 34, 56, 78, 90, 13],
name='james steve jack ted eric bob'.split(),
remark='',
))
df2 = pd.DataFrame(dict(
id=[12, 34, 56, 78, 13],
name='james steve jack ted bob'.split(),
remark='',
))
df2.at[2, 'remark'] = 'smart'
解決方案
df2.append(df1).drop_duplicates(['id', 'name']).reset_index(drop=True)
id name remark
0 12 james
1 34 steve
2 56 jack smart
3 78 ted
4 13 bob
5 90 eric
您是否在尋找'pd.concat([DF2,DF1 [〜df1.Id.isin(df2.Id )]],axis = 0) '? – Wen