Filter a column based on another column in another df

Question:

Say I have a df where time is in time format.

Time lat lon
1/10/2022 11:30 22.12 113.15
1/10/2022 11:32 22.13 113.14
1/10/2022 11:45 22.15 113.19
1/10/2022 12:01 22.11 113.13
1/10/2022 12:11 22.17 113.19

Here is the code

    df['Time']= pd.to_datetime(df['Time'])
    df['Time'] = df['Time'].dt.time

Now, I would like to filter those rows which contain the following time:

timeList = {'Time': ['1/10/2022 11:30', '1/10/2022 11:45', '1/10/2022 12:01']}
    dates = pd.DataFrame(data=timeList)
    dates['Time'] = pd.to_datetime(dates.stack(), format='%d/%m/%Y %H:%M').unstack()
    dates['Time'] = dates['Time'].dt.time

I tried to use isin but it only works with list.
Is there a simple way I can do this?

The expected result

Time lat lon
1/10/2022 11:30 22.12 113.15
1/10/2022 11:45 22.15 113.19
1/10/2022 12:01 22.11 113.13
Asked By: user16971617

||

Answers:

If you want to use isin it is a better idea to use str:

df[df['Time'].astype(str).isin(dates['Time'].astype(str))]
Answered By: Nuri Taş
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.