Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 连接数据帧行并在键相同时匹配_Python_Pandas_Merge_Concatenation - Fatal编程技术网

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

Python 连接数据帧行并在键相同时匹配,python,pandas,merge,concatenation,Python,Pandas,Merge,Concatenation,我有两个数据帧,df1和df2,我正试图找出一种生成df3的方法,如屏幕截图所示: 因此,这里的目标是保留df1的所有行,并在它们下面追加df2的行。但是,我希望有一行用于匹配名称Lat和Lon。因此,Name、Lat和Lon将用作键 还有ZIP列的问题。对于连接的行,我希望保留df1的ZIP值 我试过: df3=pandas.merge(df1,df2,on=['Name','Lat','Lon'],how='outer') 这产生了一些接近我想要的东西: 如您所见,上面的datafra

我有两个数据帧,df1和df2,我正试图找出一种生成df3的方法,如屏幕截图所示:

因此,这里的目标是保留df1的所有行,并在它们下面追加df2的行。但是,我希望有一行用于匹配名称Lat和Lon。因此,Name、Lat和Lon将用作键

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

我试过:

df3=pandas.merge(df1,df2,on=['Name','Lat','Lon'],how='outer')
这产生了一些接近我想要的东西:

如您所见,上面的dataframe提供了两个不同的ZIP和Address列


关于如何获得干净的df3数据帧有什么想法吗?

我认为“合并”不适合这个任务(即,在右DF上加入左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)

让我知道这是否有效。

对不起,我不得不接受你的回答。我意识到这不是我想要的。例如,时钟需要从两个数据帧中同时获取评论和公共列的值。因此,应该有一个合并操作涉及。