Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python Pandas – Filter DataFrame between two dates
To filter DataFrame between two dates, use the dataframe.loc method. This allows you to select rows based on date conditions using boolean indexing.
Creating a DataFrame with Date Records
At first, import the required library and create a DataFrame with date columns ?
import pandas as pd
# Dictionary of lists with date records
d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'],
'Date_of_Purchase': ['2021-07-10', '2021-08-12', '2021-06-17',
'2021-03-16', '2021-02-19', '2021-08-22']}
# Creating dataframe from the above dictionary of lists
dataFrame = pd.DataFrame(d)
print("DataFrame...")
print(dataFrame)
DataFrame...
Car Date_of_Purchase
0 BMW 2021-07-10
1 Lexus 2021-08-12
2 Audi 2021-06-17
3 Mercedes 2021-03-16
4 Jaguar 2021-02-19
5 Bentley 2021-08-22
Filtering Between Two Dates
To filter cars purchased between two specific dates (2021-05-10 and 2021-08-25), use the loc method with boolean conditions ?
import pandas as pd
# Dictionary of lists with date records
d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'],
'Date_of_Purchase': ['2021-07-10', '2021-08-12', '2021-06-17',
'2021-03-16', '2021-02-19', '2021-08-22']}
dataFrame = pd.DataFrame(d)
# Filter cars purchased between two dates
resDF = dataFrame.loc[(dataFrame["Date_of_Purchase"] >= "2021-05-10") &
(dataFrame["Date_of_Purchase"] <= "2021-08-25")]
print("Cars purchased between 2021-05-10 and 2021-08-25:")
print(resDF)
Cars purchased between 2021-05-10 and 2021-08-25:
Car Date_of_Purchase
0 BMW 2021-07-10
1 Lexus 2021-08-12
2 Audi 2021-06-17
5 Bentley 2021-08-22
How It Works
The filtering uses boolean indexing with these components:
-
dataFrame["Date_of_Purchase"] >= "2021-05-10"? Checks if date is greater than or equal to start date dataFrame["Date_of_Purchase"] ? Checks if date is less than or equal to end date-
&operator ? Combines both conditions (both must be True) -
dataFrame.loc[]? Selects rows where the boolean condition is True
Alternative Methods
You can also use between() method for cleaner syntax ?
import pandas as pd
d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'],
'Date_of_Purchase': ['2021-07-10', '2021-08-12', '2021-06-17',
'2021-03-16', '2021-02-19', '2021-08-22']}
dataFrame = pd.DataFrame(d)
# Using between() method
resDF = dataFrame[dataFrame["Date_of_Purchase"].between("2021-05-10", "2021-08-25")]
print("Using between() method:")
print(resDF)
Using between() method:
Car Date_of_Purchase
0 BMW 2021-07-10
1 Lexus 2021-08-12
2 Audi 2021-06-17
5 Bentley 2021-08-22
Conclusion
Use dataFrame.loc[] with boolean conditions or between() method to filter DataFrame rows between two dates. The between() method provides cleaner syntax for date range filtering.
Advertisements
