Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.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 - Fatal编程技术网

Python 熊猫合并-基于键引入相同的列值

Python 熊猫合并-基于键引入相同的列值,python,pandas,merge,Python,Pandas,Merge,我有3个这样的数据帧 df = pd.DataFrame([[1, 3], [2, 4], [3,6], [4,12], [5,18]], columns=['A', 'B']) df2 = pd.DataFrame([[1, 5], [2, 6], [3,9]], columns=['A', 'C']) df3 = pd.DataFrame([[4, 15, "hello"], [5, 19, "yes"]], columns=['A', 'C', 'D']) A B C_x

我有3个这样的数据帧

df = pd.DataFrame([[1, 3], [2, 4], [3,6], [4,12], [5,18]], columns=['A', 'B'])
df2 = pd.DataFrame([[1, 5], [2, 6], [3,9]], columns=['A', 'C'])
df3 = pd.DataFrame([[4, 15, "hello"], [5, 19, "yes"]], columns=['A', 'C', 'D'])
    A   B   C_x  C_y       D
0   1   3   5.0  NaN      NaN
1   2   4   6.0  NaN      NaN
2   3   6   9.0  NaN      NaN
3   4   12  NaN  15.0     hello
4   5   18  NaN  19.0     yes
    A   B   C        D
0   1   3   5.0     NaN
1   2   4   6.0     NaN
2   3   6   9.0     NaN
3   4   12  15.0    hello
4   5   18  19.0    yes
它们看起来像这样, df

df2

df3

我的合并,第一次合并

f_merge = pd.merge(df, df2, on='A',how='left')
第二次合并,第一次与df3合并

我得到这样的输出

df = pd.DataFrame([[1, 3], [2, 4], [3,6], [4,12], [5,18]], columns=['A', 'B'])
df2 = pd.DataFrame([[1, 5], [2, 6], [3,9]], columns=['A', 'C'])
df3 = pd.DataFrame([[4, 15, "hello"], [5, 19, "yes"]], columns=['A', 'C', 'D'])
    A   B   C_x  C_y       D
0   1   3   5.0  NaN      NaN
1   2   4   6.0  NaN      NaN
2   3   6   9.0  NaN      NaN
3   4   12  NaN  15.0     hello
4   5   18  NaN  19.0     yes
    A   B   C        D
0   1   3   5.0     NaN
1   2   4   6.0     NaN
2   3   6   9.0     NaN
3   4   12  15.0    hello
4   5   18  19.0    yes
我需要像这样

df = pd.DataFrame([[1, 3], [2, 4], [3,6], [4,12], [5,18]], columns=['A', 'B'])
df2 = pd.DataFrame([[1, 5], [2, 6], [3,9]], columns=['A', 'C'])
df3 = pd.DataFrame([[4, 15, "hello"], [5, 19, "yes"]], columns=['A', 'C', 'D'])
    A   B   C_x  C_y       D
0   1   3   5.0  NaN      NaN
1   2   4   6.0  NaN      NaN
2   3   6   9.0  NaN      NaN
3   4   12  NaN  15.0     hello
4   5   18  NaN  19.0     yes
    A   B   C        D
0   1   3   5.0     NaN
1   2   4   6.0     NaN
2   3   6   9.0     NaN
3   4   12  15.0    hello
4   5   18  19.0    yes
我如何实现这个输出?任何建议都很好。

我们可以先做合并

我们可以先做联合检查


合并前先合并df2和df3

new_df = pd.merge(df, pd.concat([df2, df3], ignore_index=True), on='A')

new_df

Out: 
   A   B   C      D
0  1   3   5    NaN
1  2   4   6    NaN
2  3   6   9    NaN
3  4  12  15  hello
4  5  18  19    yes

合并前先合并df2和df3

new_df = pd.merge(df, pd.concat([df2, df3], ignore_index=True), on='A')

new_df

Out: 
   A   B   C      D
0  1   3   5    NaN
1  2   4   6    NaN
2  3   6   9    NaN
3  4  12  15  hello
4  5  18  19    yes