floss
floss

Reputation: 2773

How to delete row if the data matches in list using Pandas

My df is an hourly dataset given below:

time                    Open
2017-01-01 00:00:00     5.2475
2017-01-01 01:00:00     5.2180
2017-01-01 02:00:00     5.2128
...., ....
2017-12-31 23:00:00     5.7388

I want to delete/remove the row if it matches the Date Series in this list:

remove = ['2017-01-01','2017-05-21', '2017-09-19']

Please note that the data in remove is a single Day whereas the data in df is hourly.

I want to remove any hourly data that matches the Day in remove

What did I do?

1: I tried df2 = df[~df.time.str.startswith(remove)] but it does not work and gives me float point error.

2: I also tried df2 = df[~df.time.isin(a)] but it only removes if it matches entirely not partially.

Could you please help me solve this?

Upvotes: 1

Views: 97

Answers (1)

Umar.H
Umar.H

Reputation: 23099

try

df2 = df[~df.time.dt.normalize().isin(remove)]

.normalize removes the time element of your data so you can match against the dates.

print(df2)


  time  Open
4   2017-12-31 23:00:00     5.7388

Upvotes: 2

Related Questions