gpt4 book ai didi

python - 可以使用比较来合并两个 Pandas 数据框吗?

转载 作者:太空狗 更新时间:2023-10-30 00:14:29 25 4
gpt4 key购买 nike

使用以下命令:

pandas.merge(df_1, df_2, left_on=['date'], right_on=['from_date'])

如果 date 中的值,我将两个表中的两行合并- 第一个表的列等于 from_date 中的值- 第二个表的列。

现在我想让它稍微复杂一点。如果 date 中的值,我需要将第一个表中的一行与第二个表中的一行合并第一个表的列等于或大于 from_date 的值- 第二个表的列且小于 upto_date 中的值- 第二列的列。

在 SQL 中,人们会使用类似的东西:

select
*
from
table_1
join
table_2
on
table_1.date >= table_2.from_date
and
table_1.date < table_2.upto_date

是否可以在 pandas 中实现。

最佳答案

pandasql是一个非常有用的工具,用于使用 SQLite 查询语法查询 pandas DataFrame。

资源

这是一个类似于您描述的示例。

导入

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pandas as pd
from pandas.io.parsers import StringIO
from pandasql import sqldf

# helper func useful for saving keystrokes
# when running multiple queries
def dbGetQuery(q):
return sqldf(q, globals())

伪造一些数据

sample_a = """timepoint,measure
2014-01-01 00:00:00,78
2014-01-03 00:00:00,5
2014-01-04 00:00:00,73
2014-01-05 00:00:00,40
2014-01-06 00:00:00,45
2014-01-08 00:00:00,2
2014-01-09 00:00:00,96
2014-01-10 00:00:00,82
2014-01-11 00:00:00,61
2014-01-12 00:00:00,68
2014-01-13 00:00:00,8
2014-01-14 00:00:00,94
2014-01-15 00:00:00,16
2014-01-16 00:00:00,31
2014-01-17 00:00:00,10
2014-01-18 00:00:00,34
2014-01-19 00:00:00,27
2014-01-20 00:00:00,75
2014-01-21 00:00:00,49
2014-01-23 00:00:00,28
2014-01-24 00:00:00,91
2014-01-25 00:00:00,88
2014-01-27 00:00:00,98
2014-01-28 00:00:00,39
2014-01-29 00:00:00,90
2014-01-30 00:00:00,63
2014-01-31 00:00:00,77
"""

sample_b = """from_date,to_date,measure
2014-01-02 00:00:00,2014-01-06 00:00:00,89
2014-01-03 00:00:00,2014-01-07 00:00:00,80
2014-01-04 00:00:00,2014-01-05 00:00:00,44
2014-01-05 00:00:00,2014-01-12 00:00:00,68
2014-01-06 00:00:00,2014-01-11 00:00:00,62
2014-01-07 00:00:00,2014-01-14 00:00:00,5
2014-01-08 00:00:00,2014-01-09 00:00:00,23
"""

读取数据集以创建 2 个 DataFrame

df1 = pd.read_csv(StringIO(sample_a), parse_dates=['timepoint'])
df2 = pd.read_csv(StringIO(sample_b), parse_dates=['from_date', 'to_date'])

编写 SQL 查询

请注意,这个使用 SQLite BETWEEN运算符(operator)。你也可以换掉它并使用类似 ON timepoint >= from_date AND timepoint < to_date 的东西如果您愿意。

query = """
SELECT
DATE(df1.timepoint) AS timepoint
, DATE(df2.from_date) AS start
, DATE(df2.to_date) AS end
, df1.measure AS measure_a
, df2.measure AS measure_b
FROM
df1
INNER JOIN df2
ON df1.timepoint BETWEEN
df2.from_date AND df2.to_date
ORDER BY
df1.timepoint;
"""

使用辅助函数运行查询

df3 = dbGetQuery(query)

df3
timepoint start end measure_a measure_b
0 2014-01-03 2014-01-02 2014-01-06 5 89
1 2014-01-03 2014-01-03 2014-01-07 5 80
2 2014-01-04 2014-01-02 2014-01-06 73 89
3 2014-01-04 2014-01-03 2014-01-07 73 80
4 2014-01-04 2014-01-04 2014-01-05 73 44
5 2014-01-05 2014-01-02 2014-01-06 40 89
6 2014-01-05 2014-01-03 2014-01-07 40 80
7 2014-01-05 2014-01-04 2014-01-05 40 44
8 2014-01-05 2014-01-05 2014-01-12 40 68
9 2014-01-06 2014-01-02 2014-01-06 45 89
10 2014-01-06 2014-01-03 2014-01-07 45 80
11 2014-01-06 2014-01-05 2014-01-12 45 68
12 2014-01-06 2014-01-06 2014-01-11 45 62
13 2014-01-08 2014-01-05 2014-01-12 2 68
14 2014-01-08 2014-01-06 2014-01-11 2 62
15 2014-01-08 2014-01-07 2014-01-14 2 5
16 2014-01-08 2014-01-08 2014-01-09 2 23
17 2014-01-09 2014-01-05 2014-01-12 96 68
18 2014-01-09 2014-01-06 2014-01-11 96 62
19 2014-01-09 2014-01-07 2014-01-14 96 5
20 2014-01-09 2014-01-08 2014-01-09 96 23
21 2014-01-10 2014-01-05 2014-01-12 82 68
22 2014-01-10 2014-01-06 2014-01-11 82 62
23 2014-01-10 2014-01-07 2014-01-14 82 5
24 2014-01-11 2014-01-05 2014-01-12 61 68
25 2014-01-11 2014-01-06 2014-01-11 61 62
26 2014-01-11 2014-01-07 2014-01-14 61 5
27 2014-01-12 2014-01-05 2014-01-12 68 68
28 2014-01-12 2014-01-07 2014-01-14 68 5
29 2014-01-13 2014-01-07 2014-01-14 8 5
30 2014-01-14 2014-01-07 2014-01-14 94 5

关于python - 可以使用比较来合并两个 Pandas 数据框吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25688524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com