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.
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.
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
Related articles
- python - if the column that is pandas is other than the specified string, i want to rewrite the string in another column
- python - i can't understand the specifications of pandas
- python - about data analysis in pandas
- python 3x - the reading of the expression in the excel file in pandas becomes nan
- python - about pandas, gspread
- python pandas pivot_table is not reflected
- python - how to load multiple time formats with pandas
- python - count by element with pivot_table in pandas
- (python) input () if time elapses without input, skip input () and move to the next process what can i do?
- python - [pandas] how to search for unexpected data
- python - apply format other than x column in pandas
- python 3x - i want to divide the process when it is err as a result of command execution in python3 subprocess
- python - when classifying other classes with lightgbm, they are all predicted to be in the same class please tell me how to deal
- i want to get attributes other than special attributes in python
- python - merge after pandas pivot
- eliminating pandas install and import in python pyenv export ldflags
- python - shuffle a few lines of pandas for weekdays and holidays
- python - when using the cv2adaptivethreshold function in the binarization process of an image, an error occurs in medianblur and
- how do i merge dataframes inside a python pandas function?
- python - the date format when reading csv data with pandas and graphing it does not work
Related questions
- python : Error reading csv
- python : Deleting rows in a dataframe according to different conditions
- python : batch rename files using Pandas
- python : How to change column and label values in txt file?
- python : How to change column type in DataFrame
- python : Merge two columns
- python : Filter specific rows with year in a dataframe (pandas)?
- python : How to import image into SQLite
- python : How do I assign names to columns in an array?
- python : How do I add new computed columns with conditions in Pandas?
Somehow I knew what my boss wanted to do ...
Does it mean that you want to process data every time two sets appear?