【问题标题】:Is there any problem with pandas merge currently?熊猫合并目前有什么问题吗?
【发布时间】:2020-01-11 07:44:11
【问题描述】:

使用外连接合并两个表。比方说

df1 = ['productID', 'Name']

df2 = ['userID', 'productID', 'usage']

我尝试在 pandas 中使用外连接和合并功能。

pd.merge(df1, df2[['userID','productID', 'usage']], on='productID', how = 'outer')

但是,我收到的错误消息是 'productID' is both an index level and a column label, which is ambiguous.

我用谷歌搜索了这条错误消息,看到了一个开放的 [问题]:https://github.com/facebook/prophet/issues/891

我的问题有什么解决办法吗?

【问题讨论】:

  • 'productID' is both an index level and a column label, which is ambiguous 表示您的索引是 productID 和一列,可能是您将 set_index() 作为任一数据帧上的产品 ID,重命名索引名称或删除同一列并使用 @987654328 @/right_index 合并参数
  • 谢谢@anky_91。下面的答案解决了我的问题。

标签: python pandas jupyter-notebook


【解决方案1】:

错误表示存在与列productID 相同的索引名称:

#check it
print (df2.index.name)

解决方案是删除/重命名索引名称,例如DataFrame.rename_axis:

pd.merge(df1, df2.rename_axis(None)[['userID','productID', 'usage']], 
          on='productID', how = 'outer')

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 2019-05-26
    • 2015-05-29
    • 2017-08-06
    • 2019-05-10
    • 2021-03-25
    • 2020-04-08
    相关资源
    最近更新 更多