【问题标题】:Pandas merge DataFrames based on index/column combinationPandas 基于索引/列组合合并 DataFrame
【发布时间】:2016-11-22 13:44:46
【问题描述】:

我有两个DataFrames 想要merge。我读过关于merging on multiple columnspreserving the index when merging 的信息。我的问题需要兼顾两者,而我很难找出最好的方法。

第一个 DataFrame 是这样的

第二个看起来像这样

我想根据Date ID 合并这些。在第一个 DataFrame 中,Date 是索引,ID 是一列;在第二个 DataFrame 中,DateID 都是 MultiIndex 的一部分。

基本上,因此我想要一个看起来像 DataFrame 2 的 DataFrame,并为 DataFrame 1 中的 Events 增加一列。

【问题讨论】:

  • 介意发布 DataFrame 的文本,这样我们就不必手动重新创建它们了吗?
  • 它们是通过从 MongoDB 读取创建的,不幸的是我无法共享该数据

标签: python pandas dataframe


【解决方案1】:

我建议重置索引 (reset_index),然后合并 DataFrame,如您所读。然后你可以设置索引(set_index)来重现你想要的MultiIndex。

【讨论】:

    猜你喜欢
    • 2018-02-05
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    相关资源
    最近更新 更多