【问题标题】:Pandas matching rows by dates between 2 dataframesPandas 按日期在 2 个数据帧之间匹配行
【发布时间】:2020-05-24 11:14:15
【问题描述】:

我有 2 个数据框。

dfA 有 848 行

dfB 有 600 行

两者都将日期列作为索引。

我想在 dfA 中找到与 dfB 中的日期匹配的 600 行——dfA 应以与 dfB 匹配的 600 行结尾。

尝试类似下面的方法但不起作用...

dfA.iloc(dfB.index)

【问题讨论】:

标签: python pandas


【解决方案1】:

使用DataFrame.reindex:

dfA.reindex(index = dfB.index)

DataFrame.loc

dfA.loc[dfB.index]

或者如果该列没有真正设置为index,您可以这样做:

dfA.set_index('Date').reindex(index = dfB['Date'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 2018-05-08
    相关资源
    最近更新 更多