Query DataFrame Rows Within a Specified Date Range
This question addresses the challenge of extracting rows within a particular date range from a DataFrame containing a date column. The provided solution offers two approaches for achieving this.
Method 1: Utilizing a Boolean Mask
To adopt this method, ensure that 'date' in your DataFrame represents a Series with dtype datetime64[ns]. Employ the following steps:
Method 2: Assigning a DatetimeIndex
Optimal for scenarios involving frequent date selections, this approach involves setting the date column as the index:
Example:
Utilizing the code provided in the response, consider the following illustration:
import pandas as pd df = pd.DataFrame({'date': pd.date_range('2023-03-01', periods=10)}) df['value'] = np.random.randn(10) # Boolean Mask Approach start_date = '2023-03-03' end_date = '2023-03-08' mask = (df['date'] > start_date) & (df['date'] <= end_date) df_subset = df.loc[mask] # DatetimeIndex Approach df = df.set_index('date') df_subset = df.loc[start_date:end_date]
This would yield two DataFrames that contain rows corresponding to the specified date range.
The above is the detailed content of How to Efficiently Filter DataFrame Rows by Date Range?. For more information, please follow other related articles on the PHP Chinese website!