Categories
Python Answers

How to select DataFrame rows between two dates with Python Pandas?

Spread the love

To select DataFrame rows between two dates with Python Pandas, we can use a boolean mask.

For instance, we write

df['date'] = pd.to_datetime(df['date'])  
mask = (df['date'] > start_date) & (df['date'] <= end_date)

to convert the 'date' column entries to datetime64 with pd.to_datetimr.

Then we create the mask with (df['date'] > start_date) & (df['date'] <= end_date).

And then we get the filtered rows between start_date and end_date with

df.loc[mask]

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *