【问题标题】:Merging one dataframe that has two date columns with another dataframe that has two date columns将具有两个日期列的一个数据框与另一个具有两个日期列的数据框合并
【发布时间】:2023-04-10 04:03:01
【问题描述】:

我很难弄清楚如何匹配包含两列时间范围和一些附加列的数据帧。我需要在 Dataframe A 中插入/匹配 start_dateend_date 到 Dataframe Bopen_dateclose_date 中的观察结果,同时匹配 other_varname

这是我拥有的两个数据框:

    A                                               B
    start_date  end_date    other_var   name            open_date   close_date   other_var   name
0   2018-05-01  2018-05-01  7H          companyB    0   2018-07-06  2019-02-17   1H          companyA   
1   2018-05-02  2018-05-04  1H          companyC    1   2018-07-13  2018-11-04   1H          companyB
2   2018-05-10  2018-05-11  2H          companyA    2   2018-04-23  2018-05-08   1H          companyB
3   2018-05-04  2018-05-09  1H          companyB    3   2018-04-05  2018-10-01   7H          companyB
                                                    4   2018-05-03  2018-06-01   1H          companyB

我想要的新数据框 (C) 中的输出是:

    C                                               
    start_date  end_date    open_date   close_date   other_var   name 
0   2018-05-01  2018-05-01  2018-04-05  2018-10-01   7H          companyB
1   2018-05-04  2018-05-09  2018-05-03  2018-06-01   1H          companyB

请注意,数据框 C 中的顺序无关紧要,我正在使用的数据集约为 1000 行,日期范围为 2015-2019。

非常感谢任何建议或帮助。谢谢大家。

【问题讨论】:

  • 您可以将要匹配的列设置为索引。然后只需按索引合并两个数据帧。
  • 为什么与start_date : 2018-05-04end_date : 2018-05-09open_date : 2018-04-05close_date : 2018-10-01不匹配。逻辑不清楚。
  • @elPastor 因为other_var 在数据帧A 和数据帧B 之间是不同的。因此,没有匹配。感谢您仍然查看它。

标签: pandas join filter merge pandas-groupby


【解决方案1】:

我建议合并other_varname,然后通过比较日期时间列进行过滤:

import pandas as pd
# Reproducing your data
dfa = pd.DataFrame({"start_date": ["2018-05-01", "2018-05-02", "2018-05-10", "2018-05-04"],
                    "end_date": ["2018-05-01", "2018-05-04", "2018-05-11", "2018-05-09"],
                    "other_var": ["7H", "1H", "2H", "1H"],
                    "name": ["companyB", "companyC", "companyA", "companyB"]})

dfb = pd.DataFrame({"open_date": ["2018-07-06", "2018-07-13", "2018-04-23", "2018-04-05", "2018-05-03"],
                    "close_date": ["2019-02-17", "2018-11-04", "2018-05-08", "2018-10-01", "2018-06-01"],
                    "other_var": ["1H", "1H", "1H", "7H", "1H"],
                    "name": ["companyA", "companyB", "companyB", "companyB", "companyB"]})

df = pd.merge(dfa, dfb, on=["other_var", "name"])
df[["start_date", "end_date", "open_date", "close_date"]] = \
    df[["start_date", "end_date", "open_date", "close_date"]].apply(pd.to_datetime)
df = df.loc[(df["start_date"]>=df["open_date"]) & (df["end_date"]<=df["close_date"]),:]

结果是

  start_date   end_date other_var      name  open_date close_date
0 2018-05-01 2018-05-01        7H  companyB 2018-04-05 2018-10-01
3 2018-05-04 2018-05-09        1H  companyB 2018-05-03 2018-06-01

【讨论】:

    猜你喜欢
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2022-12-19
    • 1970-01-01
    • 2021-10-05
    相关资源
    最近更新 更多