【问题标题】:compare column values only with identical datetime index仅比较具有相同日期时间索引的列值
【发布时间】:2020-04-14 01:03:09
【问题描述】:

我有一个从 07:00:00 到 20:00:00 (df1) 的长 df 和一个短 df,只有长的一部分 (df2)(相同的日期时间索引值)。

我想比较两个数据框的分组大小值。

日期时间索引、id、x 和 y 值应该相同。

我可以这样做吗?

df1:

Out[180]: 

date                     id       gs   x    y                                          
2019-10-09 07:38:22.139  3166     nan  248  233
2019-10-09 07:38:25.259  3166     nan  252  235
2019-10-09 07:38:27.419  3166     nan  253  231
2019-10-09 07:38:30.299  3166     nan  251  232
2019-10-09 07:38:32.379  3166     nan  251  233
2019-10-09 07:38:37.179  3166     nan  228  245
2019-10-09 07:39:49.498  3167     nan  289  253
2019-10-09 07:40:19.099  3168     nan  288  217
2019-10-09 07:40:38.779  3169     nan  278  139
2019-10-09 07:40:39.899  3169     nan  279  183
...
2019-10-09 19:52:53.959  5725     nan  190  180
2019-10-09 19:52:56.439  5725     nan  193  185
2019-10-09 19:52:58.919  5725     nan  204  220
2019-10-09 19:53:06.440  5804     nan  190  198
2019-10-09 19:53:08.919  5804     nan  200  170
2019-10-09 19:53:11.419  5804     nan  265  209
2019-10-09 19:53:16.460  5789     nan  292  218
2019-10-09 19:53:36.460  5806     nan  284  190
2019-10-09 19:54:08.939  5807     nan  404  226
2019-10-09 19:54:23.979  5808     nan  395  131

df2:

Out[181]: 

date                     id    gs   x    y                                       
2019-10-09 11:20:01.418  3479  2.0  353  118.0
2019-10-09 11:20:01.418  3477  2.0  315   92.0
2019-10-09 11:20:01.418  3473  2.0  351  176.0
2019-10-09 11:20:01.418  3476  2.0  318  176.0
2019-10-09 11:20:01.418  3386  0.0  148  255.0
2019-10-09 11:20:01.418  3390  0.0  146  118.0
2019-10-09 11:20:01.418  3447  0.0  469  167.0
2019-10-09 11:20:03.898  3447  0.0  466  169.0
2019-10-09 11:20:03.898  3390  0.0  139  119.0
2019-10-09 11:20:03.898  3477  2.0  316   93.0

预期输出应该是包含“date”、“id”、“x”、“y”、“gs(df1)”、“gs(df2)”列的数据框

【问题讨论】:

    标签: python pandas dataframe datetime


    【解决方案1】:

    在一切都相等的情况下进行合并,但请确保重置索引,使其成为合并条件的一部分

    df1_t = df1.reset_index()
    df2_t = df1.reset_index()
    
    results = df1_t.merge(df2_t, left_on = ['date', 'ids', 'x', 'y'],
                          right_on = ['date', 'ids', 'x', 'y'], 
                          indicator = True).reset_index()
    
    print(results)
    

    results 将在 df1 上包含 df2 中的行。

    【讨论】:

    • df2_temp 在这里是什么?
    • 对不起是一个类型,是df2
    • 非常感谢!但它给了我 ValueError: Can only compare the same-labeled DataFrame objects。所以我有两个数据时间索引,而 df2 是 df1 的一部分,其中 dateitme 索引是相同的。我只想要 df1 中具有相同索引、id、x 和 y 的数据行
    • 哦,检查我所做的编辑,我的错误是我理解错误。
    猜你喜欢
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 2019-12-06
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多