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.
Assuming you have non-date columns and date-like columns, you could convert them to date with pd.to_datetime
and errors='coerce'
. Select the non-date columns with isna
, and the wanted dates with between
, then perform boolean indexing on the columns and select them:
dates = pd.to_datetime(df.columns, errors='coerce', format='%d/%m/%Y')
m = dates.to_series().between(pd.Timestamp('2025-01-04'),
pd.Timestamp('2025-01-06'),
inclusive='both')
out = df.loc[:, dates.isna() | m.values]
Output:
Fname Lname 04/01/2025 05/01/2025 06/01/2025
0 Owen Richardson 128 114 239
1 Edward Jones 148 144 182
2 Steven Cameron 228 272 140
3 Aldus Turner 281 139 171
4 Dainton Wright 269 176 142
5 Sofia Harrison 100 103 154
6 Heather Evans 155 163 201
7 Stella Harris 126 183 157
8 Joyce Smith 251 143 229
9 Tyler Hill 299 293 218
If you just want the date-like:
df[df.columns[m]]
04/01/2025 05/01/2025 06/01/2025
0 128 114 239
1 148 144 182
2 228 272 140
3 281 139 171
4 269 176 142
5 100 103 154
6 155 163 201
7 126 183 157
8 251 143 229
9 299 293 218