gpt4 book ai didi

python - 如果 Pandas 中没有重复项,则将行值从一个 df append 到另一个

转载 作者:行者123 更新时间:2023-12-03 21:08:18 25 4
gpt4 key购买 nike

我有两个 dfs


df1 = pd.DataFrame({'pupil': ["sarah", "john", "fred"],
'class': ["1a", "1a", "1a"]})


df2 = pd.DataFrame({'pupil_mixed': ["sarah", "john", "lex"],
'class': ["1a", "1c", "1a"]})


我想从 df2 的“pupil_mixed”列中 append 行值
如果值不重复,则转到 df1 中的“瞳孔”列
期望的结果:
df1 = pd.DataFrame({'pupil': ["sarah", "john", "fred", 'lex'],
'class': ["1a", "1a", "1a", NaN]})


我用过 appendloc df1 = df1.append(df2.loc[df2['pupil_mixed'] != df1['pupil'] ])它只是将另一列 append 到具有匹配行值的 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)
输出
   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 参数,有效地更改列名称。

关于python - 如果 Pandas 中没有重复项,则将行值从一个 df append 到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65446572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com