小能豆

Select DataFrame rows between two dates

py

I am creating a DataFrame from a csv as follows:

stock = pd.read_csv('data_in/' + filename + '.csv', skipinitialspace=True)

The DataFrame has a date column. Is there a way to create a new DataFrame (or just overwrite the existing one) which only contains rows with date values that fall within a specified date range or between two specified date values?


阅读 65

收藏
2023-12-23

共1个答案

小能豆

Yes, you can filter a DataFrame based on a date range using the Pandas library in Python. Here’s an example of how you can create a new DataFrame or overwrite the existing one with rows that fall within a specified date range:

import pandas as pd

# Assuming your 'date' column is in datetime format
stock['date'] = pd.to_datetime(stock['date'])

# Specify the date range
start_date = '2023-01-01'
end_date = '2023-12-31'

# Filter rows based on the date range
filtered_stock = stock[(stock['date'] >= start_date) & (stock['date'] <= end_date)]

# If you want to overwrite the existing DataFrame
stock = filtered_stock.copy()

# Alternatively, if you want to keep the original DataFrame and just create a new one
# filtered_stock = stock[(stock['date'] >= start_date) & (stock['date'] <= end_date)]

Make sure to replace ‘date’ with the actual name of your date column. The pd.to_datetime function is used to convert the ‘date’ column to a datetime format if it’s not already in that format.

The filtered_stock DataFrame will contain only the rows that fall within the specified date range. You can choose whether to overwrite the original DataFrame or create a new one based on your requirements.

2023-12-23