I am quite new to Pandas. I need to select/locate the records between 2 dates.
I have tried a range of methods, but cant seem to get it. I have included a cut down of sample of the CSV/Data I am working with.
Each column is a date, so all of the documentation I have found don't match this data structure
Thanks for any help.
Here is the script to select the records between dates. This code should be little bit faster:
import pandas as pd
file_path = "file.xlsx" # Update with the correct file path
df = pd.read_excel(file_path)
# Please change the dates according to your need (04/01/2025 to 06/01/2025).
selected_columns = df[["Fname"] + ["Lname"] + list(df.loc[:, "04/01/2025":"06/01/2025"].columns)]
print(selected_columns)
If you don't need Fname and Lname please remove "["Fname"] + ["Lname"] + ". Just use the line below
selected_columns = df[list(df.loc[:, "04/01/2025":"06/01/2025"].columns)]
If you want to run the script preventing an error if any column is missing, please use:
try:
date_columns = list(df.loc[:, "04/01/2025":"06/01/2025"].columns)
except KeyError:
print("Error: The specified date range columns do not exist in the dataset.")
date_columns = [] # Prevents errors in the next step
selected_columns = df[["Fname"] + date_columns]
Output