[英]Joining two dataframes without exactly match value
Ticker & Date are index代码和日期是索引
Dataframe-A数据框-A
Ticker股票代码 | Date日期 | Renue收入 |
---|---|---|
AAPL美国航空航天局 | 2022-01-01 2022-01-01 | 10M 10M |
AAPL美国航空航天局 | 2022-04-01 2022-04-01 | 10M 10M |
MSFT微软公司 | 2022-01-01 2022-01-01 | 10M 10M |
MSFT微软公司 | 2022-04-01 2022-04-01 | 10M 10M |
Dataframe-B数据框-B
Ticker股票代码 | Date日期 | Price价格 |
---|---|---|
AAPL美国航空航天局 | 2022-01-02 2022-01-02 | 11 11 |
AAPL美国航空航天局 | 2022-01-03 2022-01-03 | 12 12 |
AAPL美国航空航天局 | 2022-04-01 2022-04-01 | 15 15 |
AAPL美国航空航天局 | 2022-04-02 2022-04-02 | 16 16 |
MSFT微软公司 | 2022-01-01 2022-01-01 | 20 20 |
MSFT微软公司 | 2022-01-02 2022-01-02 | 21 21 |
MSFT微软公司 | 2022-01-03 2022-01-03 | 22 22 |
MSFT微软公司 | 2022-04-02 2022-04-02 | 26 26 |
MSFT微软公司 | 2022-04-03 2022-04-03 | 26 26 |
Join the Dataframe by Ticker and Date通过 Ticker 和 Date 加入 Dataframe
But AAPL without 2022-01-01 Price, use 2022-01-02但 AAPL 没有 2022-01-01 价格,使用 2022-01-02
Result结果
Ticker股票代码 | Date日期 | Renue收入 | Price价格 |
---|---|---|---|
AAPL美国航空航天局 | 2022-01-01 2022-01-01 | 10M 10M | 11 11 |
AAPL美国航空航天局 | 2022-04-01 2022-04-01 | 10M 10M | 15 15 |
MSFT微软公司 | 2022-01-01 2022-01-01 | 10M 10M | 20 20 |
MSFT微软公司 | 2022-04-01 2022-04-01 | 10M 10M | 26 26 |
How to join the DatFrame to produce the result Dataframe?如何加入DatFrame产生结果Dataframe?
Thanks a lot!非常感谢!
here is one way to do it这是一种方法
it is case of less than or equal join.这是小于或等于连接的情况。 We first merge the two DFs, on Ticker, and then filter(query) where the date among two matches or DF1 date is less than the second DF date.我们首先合并两个 DF,在 Ticker 上,然后过滤(查询)两个匹配中的日期或 DF1 日期小于第二个 DF 日期的地方。 We then groupby Ticket, and date and take the first row for each group.然后,我们按 Ticket 进行分组,并为每个组确定日期并占据第一行。 Finally, reset index and drop the date from right column最后,重置索引并从右列中删除日期
df1.merge(df2, on='Ticker',
how='left',
suffixes=('','_y')
).query('Date<=Date_y').groupby(
['Ticker','Date']).first().reset_index().drop(columns='Date_y')
Ticker Date Renue Price
0 AAPL 2022-01-01 10M 11
1 AAPL 2022-04-01 10M 15
2 MSFT 2022-01-01 10M 20
3 MSFT 2022-04-01 10M 26
You can use merge as given below,您可以使用如下所示的合并,
>>> data1 = pd.DataFrame({"col":range(11, 17), # Create first
pandas DataFrame
... "x1":range(12, 1, - 2),
... "x2":["a", "b", "c", "d", "e", "f"],
... "x3":range(17, 11, - 1)})
>>> print(data1)
col x1 x2 x3
0 11 12 a 17
1 12 10 b 16
2 13 8 c 15
3 14 6 d 14
4 15 4 e 13
5 16 2 f 12
>>> data2 = pd.DataFrame({"col":range(15, 19), # Create second
pandas DataFrame
... "y1":["l", "k", "j", "h"],
... "y2":["x", "y", "y", "y"],
... "y3":range(18, 10, - 2)})
>>> print(data2) # Print second
pandas DataFrame
col y1 y2 y3
0 15 l x 18
1 16 k y 16
2 17 j y 14
3 18 h y 12
>>> data1
col x1 x2 x3
0 11 12 a 17
1 12 10 b 16
2 13 8 c 15
3 14 6 d 14
4 15 4 e 13
5 16 2 f 12
>>> data2
col y1 y2 y3
0 15 l x 18
1 16 k y 16
2 17 j y 14
3 18 h y 12
>>> data_join = pd.merge(data1, # Inner join
... data2,
... on = "col")
>>> data_join
col x1 x2 x3 y1 y2 y3
0 15 4 e 13 l x 18
1 16 2 f 12 k y 16
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.