python - Pandas 在索引列上合并?

标签 python 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

因此,如果一个 dataframe 中的 index 列与第二个 dataframe 中的另一列同名,panda 将无法合并?

最佳答案

您需要明确指定如何连接表。默认情况下,merge 将选择公共(public)列名作为合并键。对于您的情况,

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

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

关于python - Pandas 在索引列上合并?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45889486/

相关文章:

python - 将包含字典的 Pandas 数据框行拆分为多行

python - is_max = s == s.max() |我应该怎么读这个?

python - 如何在条件检查时从现有数据帧复制数据帧中的选定行? [Python]

python - Seaborn tsplot 不显示 CI strip

python - pandas:将 CSV 系列转换为数据框

python - 如何在 python 中转换分组数据框

python - 在 Tkinter 按钮内更新 Python 字典中的值

python - 如何从数据库动态获取表?

python - 使用 Mock python 断言对方法的调用

带有节点 : __str__ method 的 Python 无序列表