1

I am having a csv file which is having a lot of entries (rows). The first column contains dates in the format mm/dd/yyyy, the second one contains time in the format hh:min:sec.

All I want is that I will provide the start date, start time, end date, end time and all the records (rows) will be listed between these parameters from the csv file...

I tried using pandas but didnt get any solution.

i am attaching an image of my csv file

The entries of csv files are as follows:

Date      Time     Mode   Dir Reserved    Speed   Enf Speed
1/1/2020  0:00:10     N   R   170          103    70
1/1/2020  0:00:20     N   R   170           70    70
1/1/2020  0:00:30     N   R   170          105    70
1/1/2020  0:00:40     N   R   170          115    70
1/1/2020  0:00:50     N   R   170           81    70
1/1/2020  0:10:00     N   R   170          127    70
1/1/2020  0:10:10     N   R   170           74    70
1/1/2020  0:10:20     N   R   170          108    70

Please help me out in how to get data between these date and times..

Thank you.

MarianD
  • 13,096
  • 12
  • 42
  • 54
Akshat Pant
  • 139
  • 12

1 Answers1

0
import pandas as pd
dataset = pd.read_csv("Path_to_your_input_dataset")  #Specify your dataset input path here

#Create a new column "DateTime" which concatenates "Date" and "Time" columns and converts the column to pandas DateTime format
dataset['DateTime'] = pd.to_datetime(dataset['Date '] + ' ' + dataset['Time'])

start_date = "2020-01-01 00:00:40" #Provide your start dateTime as input here in the same exact format
end_date = "2020-01-01 00:10:10" #Provide your end dateTime as input here in the same exact format

#greater than the start date and smaller than the end date
mask = (dataset['DateTime'] >= start_date) & (dataset['DateTime'] <= end_date)

#Assign the selected mask to a new dataframe
selected_dataset = dataset.loc[mask]
print(selected_dataset)

Output:

      Date      Time Mode Dir  Reserved  Speed  Enf Speed            DateTime
3  01/01/20  0:00:40    N   R       170    115         70 2020-01-01 00:00:40
4  01/01/20  0:00:50    N   R       170     81         70 2020-01-01 00:00:50
5  01/01/20  0:10:00    N   R       170    127         70 2020-01-01 00:10:00
6  01/01/20  0:10:10    N   R       170     74         70 2020-01-01 00:10:10

Cheers!

Karthick Mohanraj
  • 1,565
  • 2
  • 13
  • 28
  • one more thing sir i want to sum Speed column..i am trying using print(selected_dataset.groupby['Speed'].sum()) but i am getting error TypeError:Method 'object' is not subscriptable..plz help me in this also – Akshat Pant May 28 '20 at 09:27
  • ```print(selected_dataset['Speed'].sum())``` – Karthick Mohanraj May 28 '20 at 09:37