python - 连接数据帧行并在键相同时匹配

标签 python pandas merge concatenation

我有两个数据框 df1 和 df2,我正在尝试找出一种生成 df3 的方法,如您在屏幕截图中所见: enter image description here

因此,这里的目标是保留 df1 的所有行并在其下附加 df2 的行。但是,我想要一行来匹配名称、纬度和经度。因此,Name、Lat 和 Lon 将用作键。

还有 ZIP 列的问题。对于连接的行,我想保留 df1 的 ZIP 值。

我试过:

df3=pandas.merge(df1,df2,on=['Name','Lat','Lon'],how='outer')

这产生了接近我想要的东西:

enter image description here

如您所见,上面的数据框有两个不同的 ZIP 和 Address 列。

关于如何获得干净的 df3 数据框的任何想法?

最佳答案

我认为“合并”不适合这项任务(即,将左侧 DF 连接到右侧 DF),因为您实际上是将一个 DF 放在另一个 DF 之上,然后删除重复项。所以你可以尝试这样的事情:

#put one DF 'on top' of the other (like-named columns should drop into place)
df3 = pandas.concat([df1, df2])
#get rid of any duplicates
df3.drop_duplicates(inplace = True)

编辑

根据您的反馈,我意识到需要一个更肮脏的解决方案。您将使用合并,然后从重复的列中填充 NaN。有点像

df1 = pd.DataFrame({'test':[1,2,3,6,np.nan, np.nan]})
df2 = pd.DataFrame({'test':[np.nan,np.nan,3,6,10,24]})

#some merge statement to get them into together into the var 'df'
df = pd.merge(df1, df2, left_index = True, right_index=True)

#collect the _x columns
original_cols = [x for x in df.columns if x.endswith('_x')]

for col in original_cols:
    #use the duplicate column to fill the NaN's of the original column
    duplicate = col.replace('_x', '_y')
    df[col].fillna(df[duplicate], inplace = True)

    #drop the duplicate
    df.drop(duplicate, axis = 1, inplace = True)

    #rename the original to remove the '_x'
    df.rename(columns = {col:col.replace('_x', '')}, inplace = True)

让我知道这是否有效。

关于python - 连接数据帧行并在键相同时匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35704619/

相关文章:

python - 从 df 创建字典的正确方法或计算 jaccard 相似度的方法

python - 将 pandas DataFrame 的 2 列与行条件相加

默认情况下 git merge -Xignore-space-change

python - 按逻辑标准将列表拆分为子列表

list - 获取两个列表元素的所有可能组合的元组列表的 Pythonic 方法是什么?

python - 值错误: Can only compare identically-labeled Series objects in pandas

用于 Python 的 Pandas,分组

git - 通过重命名其中一个文件来解决 git merge 冲突

merge - 如何处理 Mercurial 中的封闭拓扑头?

python - 使用 float ('nan' ) 来表示缺失值 - 安全吗?