python Pandas : How to combine or merge two difrent size dataframes based on dates

标签 python pandas dataframe merge

我喜欢根据日期范围合并或组合两个不同大小的数据帧 df1 和 df2,例如:
df1:

Date        Open  High  Low
2021-07-01  8.43  8.44  8.22 
2021-07-02  8.36  8.4   8.28
2021-07-06  8.22  8.23  8.06
2021-07-07  8.1   8.19  7.98
2021-07-08  8.07  8.1   7.91
2021-07-09  7.97  8.11  7.92
2021-07-12  8     8.2   8
2021-07-13  8.15  8.18  8.06
2021-07-14  8.18  8.27  8.12
2021-07-15  8.21  8.26  8.06
2021-07-16  8.12  8.23  8.07
df2:
Day of month   Revenue   Earnings
01             45000     4000
07             43500     5000
12             44350     6000
15             39050     7000
结果应该是这样的:
组合:
Date        Open  High  Low   Earnings
2021-07-01  8.43  8.44  8.22  4000
2021-07-02  8.36  8.4   8.28  4000
2021-07-06  8.22  8.23  8.06  4000
2021-07-07  8.1   8.19  7.98  5000
2021-07-08  8.07  8.1   7.91  5000
2021-07-09  7.97  8.11  7.92  5000
2021-07-12  8     8.2   8     6000
2021-07-13  8.15  8.18  8.06  6000
2021-07-14  8.18  8.27  8.12  6000
2021-07-15  8.21  8.26  8.06  7000
2021-07-16  8.12  8.23  8.07  7000
Earnings 列是根据日期范围合并的,我如何在 python pandas 中执行此操作?

最佳答案

试试 merge_asof

#df1.date=pd.to_datetime(df1.date)
df1['Day of month'] = df1.Date.dt.day 
out = pd.merge_asof(df1, df2, on ='Day of month', direction = 'backward')
out
Out[213]: 
         Date  Open  High   Low  Day of month  Revenue  Earnings
0  2021-07-01  8.43  8.44  8.22           1    45000      4000
1  2021-07-02  8.36  8.40  8.28           2    45000      4000
2  2021-07-06  8.22  8.23  8.06           6    45000      4000
3  2021-07-07  8.10  8.19  7.98           7    43500      5000
4  2021-07-08  8.07  8.10  7.91           8    43500      5000
5  2021-07-09  7.97  8.11  7.92           9    43500      5000
6  2021-07-12  8.00  8.20  8.00          12    44350      6000
7  2021-07-13  8.15  8.18  8.06          13    44350      6000
8  2021-07-14  8.18  8.27  8.12          14    44350      6000
9  2021-07-15  8.21  8.26  8.06          15    39050      7000
10 2021-07-16  8.12  8.23  8.07          16    39050      7000

关于 python Pandas : How to combine or merge two difrent size dataframes based on dates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68531077/

相关文章:

python - Pandas - 合并具有相同子字符串的所有列

python - 使用 pandas.IntervalIndex 作为数据帧索引

python - 基于条件的格式编号

python - 在两个数据框中按日期标记

python - 如何根据条件删除 pandas 值并相应地移动行

python - 将一列的数据帧与具有一系列列的另一个数据帧匹配并提取列标题 - Python

Python 获取 http 预告片

python - 聚合和填充缺失日期(天和小时)的数据

python - 使用Python中不同大小网格点的数据生成3D曲面图

python - 比较两个 anaconda 安装之间的包