Home>
Current status

A program that uses pandas to read csv files and perform calculations when ids in the id column are the same
It was created.

Problem

But my boss later told me to make a program that could read line by line.
I read on the website that it's difficult to read line by line with pandas.

Previous program
import pandas as pd
#argument
ip = input ("Enter the csv file name to be calculated (example: test5.csv):")
print (ip)
distance = int (input ("Enter the distance (meters):"))
print (distance)
mes = int (input ("Please determine the speed unit to calculate 1: Hour 2: Minute 3: Third speed"))

Read #csv file
df = pd.read_csv (f "./ {ip}", parse_dates = ['datetime'])

If there is the same #id, it is calculated by the value of mes
if (mes == 1):
    df ['speed'] = df.groupby ('id') ['datetime']. apply (lambda d: distance/d.diff (). shift (-1) .dt.total_seconds ());
    df ['speed']/= 3600;
elif (mes == 2):
    df ['speed'] = df.groupby ('id') ['datetime']. apply (lambda d: distance/d.diff (). shift (-1) .dt.total_seconds ());
    df ['speed']/= 60;
elif (mes == 3):
    df ['speed'] = df.groupby ('id') ['datetime']. apply (lambda d: distance/d.diff (). shift (-1) .dt.total_seconds ());
else:
    print ("Please enter the correct value");
    pass;

Delete #csv files other than those showing speed
df2 = df.dropna (how = 'any')
print (df2)
#Write csv file (write the value of speed per second to csv file)
df2.to_csv ('test4.csv')
Terminal when running the previous program
TestuserMacBook-Pro: Desktop test $python test5.py
Enter the csv file name to calculate (Example: test5.csv): test6.csv
test6.csv
Enter the distance (meters): 45
45
Decide the speed unit to calculate 1: Speed ​​2: Minute 3: 3: Speed ​​1
                 datetime label direction id x y w h speed
0 2019-02-21 17: 15: 14.500 Lane 1 right 9 719 407 147 175 0.010417
3 2019-02-21 17: 16: 11.600 Lane 1 right 213 746 504 141 142 0.011364
6 2019-02-21 17: 19: 10.300 Lane 1 right 533 851 396 140 161 0.011364
TestuserMacBook-Pro: Desktop test $
Now-made program
import csv
a = input ("Please enter the csv file name to calculate (ex: test5.csv):")
with open (a, newline = '') as csvfile:
     reader = csv.DictReader (csvfile)
     for row in reader:
         print (row ['id'], row ['datetime'])
  • Answer # 1

    Somehow I knew what my boss wanted to do ...
    Does it mean that you want to process data every time two sets appear?

    from dateutil.parser import parse
    filename = input ("Please enter the csv file name to be calculated (example: test5.csv):")
    distance = int (input ("Enter the distance (meters):"))
    unit = input ("Determine the speed unit to calculate 1: speed 2: minutes 3: seconds")
    UNITS = {"1": 3600, "2": 60, "3": 1}
    if unit not in UNITS:
        print ("Please enter the correct value");
        exit ();
    unit = UNITS [unit]
    HEADER_OF_ID = 'id'
    HEADER_OF_TIME = 'datetime'
    id_time = {}
    with open (f "./ {filename}") as lines:
        header = next (lines) .split (',')
        ID = header.index (HEADER_OF_ID)
        TIME = header.index (HEADER_OF_TIME)
        for line in lines:
            items = line.split (',')
            id_ = items [ID]
            time = parse (items [TIME])
            if id_ in id_time:
                print (id_, distance/((time-id_time [id _]). seconds/unit))
                del id_time [id_]
            else:
                id_time [id_] = time