Filter Pandas DataFrame by Time
Last Updated :
24 Feb, 2021
In this article let’s see how to filter pandas data frame by date. So we can filter python pandas data frame by date using the logical operator and loc() method. In the below examples we have a data frame that contains two columns the first column is Name and another one is DOB.
Example 1: filter data that’s DOB is greater than 1999-02-5.
Python
import pandas as pd
Data = { 'Name' : [ 'Mukul' , 'Rohan' , 'Mayank' ,
'Shubham' , 'Aakash' ],
'DOB' : [ '1997-04-24' , '1998-05-25' , '1999-04-11' ,
'2000-11-15' , '1998-06-28' ]}
df = pd.DataFrame(Data)
print (df)
New_df = df.loc[df[ "DOB" ] > = "1999-02-5" ]
print (New_df)
|
Output:
Example 2: filter data between two date.
Python
import pandas as pd
Data = { 'Name' : [ 'Mukul' , 'Rohan' , 'Mayank' ,
'Shubham' , 'Aakash' ],
'DOB' : [ '1997-04-24' , '1998-05-25' , '1999-04-11' ,
'2000-11-15' , '1998-06-28' ]}
df = pd.DataFrame(Data)
print (df)
Date1 = df[ "DOB" ] > = "1998-04-24"
Date2 = df[ "DOB" ] < = "1999-1-31"
New_df = df.loc[Date1 & Date2]
print (New_df)
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...