Search code examples
pythonpandasdatabasecsvextract

How to extract Data by date in python Pandas


I am trying to extract data by certain dates (for example 06/20/2021 - 06/30/2021). Right now it reads the CSV file, sorts the data by date, and finds any duplicates. The next step is to extract all data by a date timeframe and am wondering how I can do this. Any help is very appreciated :). This is what I have below:

import pandas as pd
from datetime import date, timedelta

#df = pd.read_excel(r"/Users/britevoxops2/Desktop/sample_date.xlsx") #reading Excel File

df = pd.read_csv(r"/Users/filename/Desktop/sample_date.csv")
print(df) #print original data
df.head()

Final_result = df.sort_values('Joining Date') #sorting date
print(Final_result)

duplicate = df[df['Name'].duplicated() == True] #finding duplicate name
print('Here are the Duplicates: \n',duplicate) 


 

Solution

  • You can convert it to the pandas datetime format and extract your required dates using

    df_date = df[(df['Joining Date'] < '23-03-21') & (df['Joining Date'] > '03-03-21')]