-1

Here is an example of the csv

17/10/2022 23:00;10
18/10/2022 00:00;10
19/10/2022 19:00;9

I want to remove specific rows depends on a date. How would you do that? Thank you so much. I would like to do it as you introduce a range of dates, and it deletes everything out of the range.

I havent tried it yet because i,m starting with python and dont know where to start

xWee
  • 1
  • 1
  • 1
    take a look at a pandas tutorial https://pandas.pydata.org/docs/getting_started/tutorials.html`and when you get use to it try to make some code that does what you need. Coming here and asking directly for the solution is not how StackOverflow works – Sembei Norimaki Nov 07 '22 at 13:50
  • Check out this q&a: https://stackoverflow.com/questions/67449203/delete-row-or-cell-in-a-csv-file – Captain Caveman Nov 07 '22 at 13:51
  • 1
    If you're just starting with Python make sure you learn the basics before playing with *pandas*. Try the *csv* and *datetime* modules – DarkKnight Nov 07 '22 at 13:51

2 Answers2

0

You as example.

tmp = pd.DataFrame({'date':['16/10/2022 23:00','17/10/2022 23:00','18/10/2022 00:00'],'val':[6,12,10]})
tmp[tmp['date']>'17/10/2022']

In this way you filter the dataframe by date value. I suggest before continuing to study the docs and learn the basics of python.

For example here there is the doc for pandas, the library to deal with dataframe in python. You can also load .csv file Docs

Will
  • 1,619
  • 5
  • 23
0

A more "traditional" approach that doesn't require external dependencies, (i.e. pandas) and focuses on learning Python essentials before getting into data science.

The following example shows how to read a CSV file and print rows whose date is between start and end.

import csv
from datetime import datetime

start = datetime(2022, 10, 17, 23, 30)
end = datetime(2022, 10, 19, 18, 30)
with open("data.csv", newline="") as csvfile:
    data = csv.reader(csvfile, delimiter=";")
    for dt, value in data:
        dt = datetime.strptime(dt, "%d/%m/%Y %H:%M")
        value = int(value)
        if start <= dt <= end:
            print(dt, value)
0x0fba
  • 1,520
  • 1
  • 1
  • 11