Problem:
A Pandas DataFrame is created from a CSV file containing a date column. The task is to extract only the rows whose date values fall within a specified date range or between two specified dates.
Solution:
There are two approaches to achieve this:
Option 1: Boolean Masking
Example:
import pandas as pd df = pd.read_csv('data.csv', parse_dates=['date']) start_date = '2022-01-01' end_date = '2022-02-28' mask = (df['date'] >= start_date) & (df['date'] <= end_date) df_filtered = df.loc[mask]
Option 2: DatetimeIndex
Example:
df = pd.read_csv('data.csv', parse_dates=['date']) df = df.set_index(['date']) df_filtered = df.loc[start_date : end_date]
Note: If parse_dates was used while reading the CSV, converting the date column to datetime64 is not necessary.
The above is the detailed content of How to Filter Pandas DataFrame Rows by Date Range?. For more information, please follow other related articles on the PHP Chinese website!