简体   繁体   中英

how do i compare date columns in two different data frames based on the same ID

pandas

I have two data frames and want to do a nested loop.

I want to iterate of each row from df1 and select col1 (id) and col2.

Then, it will take the ID and iterate through df2 and check if the row has the same ID and then compare date column from df1 with date column in df2

if col2 in df1 is less than col2 in df2, it will return True and append that to the row of df1.

essentially what i'm trying to do is or, if there's a faster way

for(row : df1){
    for(row : df2){
        if (df1.row[col1] == df2.row[col1]){
            if(df1.row[col2] < df2.row[col2])
                return df1.row[col3] == True
             else
                row[col3] == False


df1
col1     col2          col3      col4
01       01/01/2018     S         True
02       11/21/2018     F         False
03       04/03/2018     C         True

df2
col1    col2           col3
01      10/01/2018       A
02      01/01/2018       A
02      01/31/2018       F
02      10/01/2018       D
02      09/01/2018       V
03      02/01/2018       W
03      07/01/2018       X

pandas.merge_asof

First, for merge_asof to work, you need to sort by the dates

df1.sort_values(['col2', 'col1'], inplace=True)
df2.sort_values(['col2', 'col1'], inplace=True)

Now we can merge

pd.merge_asof(
    df1, df2.rename(columns={'col3': 'col4'}),
    on='col2', by='col1', direction='forward'
).assign(col4=lambda d: d.col4.notna())

   col1       col2 col3   col4
0     1 2018-01-01    S   True
1     3 2018-04-03    C   True
2     2 2018-11-21    F  False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM