pandas 在索引列上合并? [英] Pandas merge on index column?

查看:113
本文介绍了 pandas 在索引列上合并?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

In [88]: c
Out[88]: 
                       Address    Name
CustomerID                            
10            Address for Mike    Mike
11          Address for Marcia  Marcia

In [89]: c.index
Out[89]: Int64Index([10, 11], dtype='int64', name='CustomerID')

In [90]: orders
Out[90]: 
   CustomerID   OrderDate
0          10  2014-12-01
1          11  2014-12-01
2          10  2014-12-01

In [91]: orders.index
Out[91]: RangeIndex(start=0, stop=3, step=1)

In [92]: c.merge(orders)
---------------------------
MergeError: No common columns to perform merge on

因此,如果一个数据框架中的 index 列与第二个数据框架中的另一列具有相同的名称,熊猫将无法合并?

So panda can't merge if index column in one dataframe has the same name as another column in a second dataframe?

推荐答案

您需要明确指定如何联接表.默认情况下,merge将选择公用列名称作为合并键.对于您的情况,

You need to explicitly specify how to join the table. By default, merge will choose common column name as merge key. For your case,

c.merge(orders, left_index=True, right_on='CustomID')

此外,请阅读 pandas.DataFrame.merge . 希望这会有所帮助.

Also, read the docs of pandas.DataFrame.merge please. Hope this would be helpful.

这篇关于 pandas 在索引列上合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆