1

I have a data frame like this

##    No - V - Date   
 -  55375 X 2020-01-23   
 -  55376 X 2020-01-24   
 -  ....           
 -  56065 X 2021-12-13   
 -  56066 X 2021-12-14

And I want data in a specific time range like only from 2021-01-01 to 2021-12-01. I'm kinda new to python and pandas so i couldn't figure it out how to trim the rest fo the data.

Sei
  • 11
  • 3

1 Answers1

0

This works:

# Importing dependencies 
import pandas as pd 
import datetime

# Reading in data
df = pd.read_csv('test2.txt')
df

# Splitting the column into different columns
df[['No','V','Date']] = df['No - V - Date'].str.split(' ',expand=True)

# Converting date column to correct datatype
df['Date']= pd.to_datetime(df['Date']).dt.date

Time Range

# Time range, you would reset this for the range of dates you want 
recent_date = datetime.date(2021, 12, 1)
older_date = datetime.date(2020, 1, 1)

# Conditions to look at dates that are within 9 years from today
df = df[(df.Date <= recent_date) & (df.Date >= older_date)]

# Viewing dataframe 
df

Output

    No - V - Date        No     V      Date
0   55375 X 2020-01-23  55375   X   2020-01-23
1   55376 X 2020-01-24  55376   X   2020-01-24
aiSolutions
  • 130
  • 1
  • 7