【发布时间】:2022-09-26 11:26:00
【问题描述】:
数据: Merging Pandas DataFrame within a specific Date Range
我想使用 pyjanitor 来引用这个 post 但如果同时与其他列合并,它不会给出。
尝试
df_1.conditional_join(
... df_2,
... (\"InvoiceDate \", \"PromotionStart \", \">=\"),
... (\"InvoiceDate \", \"PromotionEnd \", \"<=\")
... )
输出
+------------+-------------+---------------+-----------+
| LocationNo | InvoiceDate | InvoiceAmount | Promotion |
+------------+-------------+---------------+-----------+
| A | 01-Jul-20 | 79 | Yes |
+------------+-------------+---------------+-----------+
| B | 01-Jul-20 | 72 | |
+------------+-------------+---------------+-----------+
| C | 01-Jul-20 | 24 | |
+------------+-------------+---------------+-----------+
| A | 02-Jul-20 | 68 | Yes |
+------------+-------------+---------------+-----------+
| B | 02-Jul-20 | 6 | Yes |
+------------+-------------+---------------+-----------+
| C | 02-Jul-20 | 27 | |
+------------+-------------+---------------+-----------+
| A | 03-Jul-20 | 25 | |
+------------+-------------+---------------+-----------+
| B | 03-Jul-20 | 62 | Yes |
+------------+-------------+---------------+-----------+
| C | 03-Jul-20 | 58 | Yes |
+------------+-------------+---------------+-----------+
| D | 03-Jul-20 | 36 | Yes |
+------------+-------------+---------------+-----------+
| E | 03-Jul-20 | 65 | |
+------------+-------------+---------------+-----------+
| F | 03-Jul-20 | 81 | |
+------------+-------------+---------------+-----------+
-
你想得到什么输出?
-
@HenryEcker 我更新了。
-
我思考您正在寻找类似
df_1.conditional_join(df_2, (\'LocationNo\', \'LocationNo\', \'==\'), (\'InvoiceDate\', \'PromotionStart\', \'>=\'), (\'InvoiceDate\', \'PromotionEnd\', \'<=\'), how=\'left\')的东西,但我不确定为什么您会在此处使用conditional_join而不是标准合并后跟where(如链接帖子中的the answer)。