Filter a pandas dataframe by start- and end-date – Ahazu.com

Filter a pandas dataframe by start- and end-date

import datetime
import pandas as pd

# If you have a dataframe df with one column: 'date' you can filter it like this:
start_date = datetime.datetime.now()
end_date = '31-12-2020'

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

filtered_results = df.loc[mask]

Leave a comment

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