Home >Backend Development >Python Tutorial >How to Filter DataFrame Rows by Date Range in Python?

How to Filter DataFrame Rows by Date Range in Python?

Susan Sarandon
Susan SarandonOriginal
2024-12-10 14:42:111067browse

How to Filter DataFrame Rows by Date Range in Python?

Select Data Frame Rows within a Specified Date Range

Problem:

How to create a new DataFrame from a CSV file that contains only rows with dates within a specified range or between two dates.

Solution 1: Using a Boolean Mask

Ensure that the DataFrame's date column is a Series with a datetime64[ns] data type. Create a boolean mask by comparing the dates to the start and end dates. Use this mask to select the rows and either create a new DataFrame or overwrite the existing one.

Example:

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

Solution 2: Using a DatetimeIndex

Convert the date column to a DatetimeIndex. This allows you to select rows by date using df.loc[start_date:end_date].

Example:

import pandas as pd
df['date'] = pd.date_range('2000-1-1', periods=200, freq='D')
df = df.set_index(['date'])
df_filtered = df.loc['2000-6-1':'2000-6-10']

Additional Notes:

  • You don't have to include both start and end dates in the index.
  • pd.read_csv has a parse_dates parameter that can be used to parse the date column during reading.

The above is the detailed content of How to Filter DataFrame Rows by Date Range in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn