如何合并两个列名不同但行数相同的数据帧?

2024-09-28 03:15:50 发布

您现在位置:Python中文网/ 问答频道 /正文

在pandas中我有两个不同的数据帧。示例:

df1=a b  df2= c
    0 1       1 
    1 2       2 
    2 3       3 

我想把它们合并在一起

^{pr2}$

我尝试使用df1['c'] = df2['c'],但是我得到了一个带有copywarnings的设置


Tags: 数据示例pandasdf1df2pr2copywarnings
1条回答
网友
1楼 · 发布于 2024-09-28 03:15:50

为了合并两个数据帧,可以使用这两个示例。两者都返回相同的目标。在

使用merge加上指示它使用索引的附加参数

试试这个:

response = pandas.merge(df1, df2, left_index=True, right_index=True)
In [2]: response
Out[2]:
    b   c
0   1   1
1   2   2
2   3   3

或者您可以使用join。以防你的daraframes有不同的索引。在

DataFrame.join is a convenient method for combining the columns of two potentially differently-indexed DataFrames into a single result DataFrame.

下面是一个基本示例:

^{pr2}$

相关问题 更多 >

    热门问题