Home > Back-end >  How to remove specific lines into one .csv?
How to remove specific lines into one .csv?

Time:11-08

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

CodePudding user response:

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

CodePudding user response:

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)
  • Related