iprof0214
iprof0214

Reputation: 701

Select dataframe rows matching date regular expression in Python

In the following dataframe, how can I select rows that have date matching '2018-06%'?

Date                     Cost   Source
2018-06-01 00:00:77      0.03   RFP
2018-06-01 00:05:77      0.01   RFP
2018-06-01 04:09:77      0.90   EFO
2018-05-02 06:99:08      0.07   THP
2018-07-01 09:05:79      0.20   RFP

Expected output

Date                     Cost   Source
2018-06-01 00:00:77      0.03   RFP
2018-06-01 00:05:77      0.01   RFP
2018-06-01 04:09:77      0.90   EFO

Upvotes: 0

Views: 719

Answers (1)

user3483203
user3483203

Reputation: 51165

It looks like you have bad timestamps, which will make it difficult to convert to datetime and search by month.

My first recommendation would be to fix that data upstream, although if that is not an option, or if you are set on using regular expressions to match, you can simply use ^2018-06:

df[df.Date.str.match(r'^2018-06')]

                  Date  Cost Source
0  2018-06-01 00:00:77  0.03    RFP
1  2018-06-01 00:05:77  0.01    RFP
2  2018-06-01 04:09:77  0.90    EFO

The regex is quite straightforward:

^           # Asserts beginning of string
2018-06     # matches your date

Upvotes: 2

Related Questions