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) & (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:
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!