熊猫数据框连接问题

时间:2020-12-19 01:51:57

标签: python pandas dataframe

仍在学习熊猫,所以如果问题看起来很幼稚,请原谅。我有两个行数不同的数据框。我想沿着轴 1 加入它们。

df1:

Myidx col1 col2 col3
0    a     b    c
1    d     e    f
2    g     h    i
3    j     k    l

df2:

idx col4  col5  col6
 0     m      n     p

生成的 df 应如下所示:

idx col1 col2 col3  col4  col5  col6
0    a     b    c    m     n     p
1    d     e    f
2    g     h    i
3    j     k    l

我可以将它附加到底部,但沿轴 1 连接它们不起作用。 我尝试使用以下两件事。 (生成的 csv 文件甚至不显示 df2)

1) df1.join(df2)
2) pd.concat([df1,df2], axis =1)

非常感谢任何帮助。

谢谢,

1 个答案:

答案 0 :(得分:0)

dfnew = df1.merge(df2, left_on='Myidx', right_on='idx', suffixes=(False, False)).reset_index()

你应该这样做

或查看合并指南。

Pandas Merging 101