代码之家  ›  专栏  ›  技术社区  ›  ojunk

pandas:不一致的连接

  •  1
  • ojunk  · 技术社区  · 8 年前

    我有两个pandas数据帧,一个日期时间戳作为索引称为“date time”,另一个浮动为列“metric1”或“metric2”。当我尝试连接它们时,会出现以下错误:

    ValueError: cannot reindex from a duplicate axis
    

    在阅读了许多小时之后,我找不到解决问题的办法(例如 What does `ValueError: cannot reindex from a duplicate axis` mean? "ValueError: cannot reindex from a duplicate axis" )

    然后我花了很长时间试图重新创建这个问题,但是我的具体数据是不可能的,但我的数据是两大张贴在这里。

    最后,数据帧的某些部分似乎不兼容,但其他部分还不错。在比较了许多不同的索引片段之后,我终于找到了不兼容的集合。有人能帮我理解为什么我不能连接这些。

    基本上我希望所有可能的时间戳都在索引中,并且有一个“metric1”和“metric2”列。如果在给定的时间戳没有列的数据,那么我们只有nan或其他东西。正常情况下 pd.concat 但在这种情况下不起作用。要重新创建,请使用:

    CSV文件:

    Test1.CSV

    timestamp,metric1
    2018-03-21 15:46:36,3.5555559999999997
    2018-03-21 15:47:36,5.345001
    2018-03-21 15:48:36,5.719998
    

    Test2.CSV

    timestamp,metric2
    2018-03-28 05:49:59,3.28
    2018-03-28 05:50:59,3.45
    2018-03-28 05:51:59,3.258332
    2018-03-28 05:52:59,3.068333
    2018-03-28 05:53:59,2.9733330000000002
    2018-03-28 05:54:59,3.0650009999999996
    2018-03-28 05:55:59,3.109999
    2018-03-28 05:56:59,3.3683330000000002
    2018-03-28 05:57:59,3.1516669999999998
    2018-03-28 05:58:59,3.051666
    2018-03-28 05:59:59,3.3083339999999994
    2018-03-28 06:01:01,3.328333
    2018-03-28 06:01:01,3.1
    2018-03-28 06:02:00,3.305
    2018-03-28 06:03:00,3.29
    2018-03-28 06:04:00,3.2183330000000003
    2018-03-28 06:05:00,3.176666
    2018-03-28 06:06:00,3.353333
    2018-03-28 06:07:00,3.3233330000000003
    2018-03-28 06:08:00,3.393332
    2018-03-28 06:09:00,3.053334
    2018-03-28 06:10:00,3.268333
    2018-03-28 06:11:00,3.239999
    2018-03-28 06:12:00,3.223332
    2018-03-28 06:13:00,3.119999
    

    Test4.CSV

    timestamp,metric2
    2018-03-21 00:00:00,10.665
    2018-03-21 00:01:00,10.285
    2018-03-21 00:02:00,10.12834
    

    注意:test2.csv和test4.csv来自完全相同的数据集。

    现在让我们加载csv文件:

    tt1 = pd.read_csv('test1.csv', index_col=0)
    tt1.index = pd.to_datetime(tt1.index)
    tt2 = pd.read_csv('test2.csv', index_col=0)
    tt2.index = pd.to_datetime(tt2.index)
    tt4 = pd.read_csv('test4.csv', index_col=0)
    tt4.index = pd.to_datetime(tt4.index)
    

    现在让我们来测试连接它们:

    无误测试

    tt3 = pd.concat([tt1, tt4], axis = 1)
    

    错误测试

    tt3 = pd.concat([tt1, tt2], axis = 1)
    ValueError: cannot reindex from a duplicate axis
    
    3 回复  |  直到 8 年前
        1
  •  2
  •   harvpan    8 年前

    你的索引重复 tt2 . 这会导致错误。 获取输出的正确方法:

    tt1.reset_index().merge(tt2.reset_index(), how='outer')
    
        2
  •  1
  •   BallpointBen    8 年前

    不要 merge , join . 参加 索引上的联接。

    tt1.join(tt2, how='outer')
    
        3
  •  -1
  •   ricko72    8 年前

    我已经解决了你的问题。

    看看这个解决方案:)

    import pandas as pd
    
    tt1 = pd.read_csv('test1.csv', index_col=0)
    tt1.index = pd.to_datetime(tt1.index)
    tt2 = pd.read_csv('test2.csv', index_col=0)
    tt2.index = pd.to_datetime(tt2.index)
    tt4 = pd.read_csv('test4.csv', index_col=0)
    tt4.index = pd.to_datetime(tt4.index)
    
    tt3 = pd.concat([tt1, tt4], axis=1)
    tt4 = tt3.reset_index().merge(tt2.reset_index(), how='outer')
    tt4 = tt4.set_index('timestamp')
    print(tt4)
    

    希望有意义