[英]pandas insert only new values from one df to another with conditions
[英]append row values from one df to another if no duplicates in pandas
我有這兩個 df
df1 = pd.DataFrame({'pupil': ["sarah", "john", "fred"],
'class': ["1a", "1a", "1a"]})
df2 = pd.DataFrame({'pupil_mixed': ["sarah", "john", "lex"],
'class': ["1a", "1c", "1a"]})
如果值不重復,我想將 append 從 df2 的“pupil_mixed”列到 df1 中的“pupil”列的行值
期望的結果:
df1 = pd.DataFrame({'pupil': ["sarah", "john", "fred", 'lex'],
'class': ["1a", "1a", "1a", NaN]})
我用append
和loc
df1 = df1.append(df2.loc[df2['pupil_mixed'] != df1['pupil'] ])
它只是將另一列附加到具有匹配行值的 df,並將不匹配的行值更改為 NaN
pupil class pupil_mixed
0 sarah 1a NaN
1 john 1a NaN
2 fred 1a NaN
2 NaN 1a lex
您可以使用concat + drop_duplicates :
res = pd.concat((df1, df2['pupil_mixed'].to_frame('pupil'))).drop_duplicates('pupil')
print(res)
Output
pupil class
0 sarah 1a
1 john 1a
2 fred 1a
2 lex NaN
作為替代方案,您可以先過濾(使用isin )然后連接:
# filter the rows in df2, rename the column pupil_mixed
filtered = df2.loc[~df2['pupil_mixed'].isin(df1['pupil'])]
# create a new single column DataFrame with the pupil column
res = pd.concat((df1, filtered['pupil_mixed'].to_frame('pupil')))
print(res)
兩種解決方案都使用to_frame和 name 參數,有效地更改列名。
# distinct df1 & df2
df1['tag'] = 1
df2['tag'] = 2
# change the column name the same
df2.columns = df1.columns
df1 = df1.append(df2)
# drop_duplicates by keep df1
df1 = df1.drop_duplicates('pupil', keep='first')
# set tag == 2, class is null
cond = df1['tag'] == 2
df1.loc[cond, 'class'] = np.nan
del df1['tag']
print(df1)
output:
print(df1)
pupil class
0 sarah 1a
1 john 1a
2 fred 1a
3 lex NaN
在 df2 中重命名pupil_mixed
后,您可以使用合並:
df1.merge(df2["pupil_mixed"].rename("pupil"), how="outer")
pupil class
0 sarah 1a
1 john 1a
2 fred 1a
3 lex NaN
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.