月关宝盒
这应该这样做:import pandas as pdfrom datetime import timedelta# create dummy datadf1 = pd.DataFrame([[1, 'X', '2014-08-17'], [1, 'Y', '2019-09-22']], columns=['IBSN', 'Type', 'Date'])df1['Date'] = pd.to_datetime(df1['Date']) # might not be necessary if your Date column already contain datetime objectsdf2 = pd.DataFrame([[2, 'X', '2014-08-16'], [2, 'D', '2019-09-22'], [9, 'X', '2014-08-18'], [3, 'H', '2019-09-22'], [3, 'Y', '2014-09-23'], [5, 'G', '2019-09-22']], columns=['IBSN', 'Type', 'Date'])df2['Date'] = pd.to_datetime(df2['Date']) # might not be necessary if your Date column already contain datetime objects# add date boundaries to the first dataframedf1['Date_from'] = df1['Date'].apply(lambda x: x - timedelta(days=1))df1['Date_to'] = df1['Date'].apply(lambda x: x + timedelta(days=1))# merge the date boundaries to df2 on 'Type'. Filter rows where date is between# data_from and date_to (inclusive). Drop 'date_from' and 'date_to' columnsdf2 = df2.merge(df1.loc[:, ['Type', 'Date_from', 'Date_to']], on='Type', how='left')df2[(df2['Date'] >= df2['Date_from']) & (df2['Date'] <= df2['Date_to'])].\ drop(['Date_from', 'Date_to'], axis=1)请注意,根据您的逻辑,df2(3 Y 2014-09-23)中的第 4 行不应保留,因为其日期(2014)不在 df1(2019 年)的给定日期之间。