I would like to use Python's pandas to load a specific column (detime) of the original csv file and add it to an existing csv file.
default_list_4.csv (Original File)
x,y,botime,detime,eldid,firev
-11321.9,-44284,0.923816,1.03909,1298,100
-11716.1,-46828.7,0.517662,0.642548,230,100
-13105.5,-42740,1.63526,2.23617,1536,100
-10864.5,-46901.3,0.997993,1.27049,668,100
list4.csv (existing file)
id, x, y, botime,eldid,firev
16286,-11321.9324063,-44284.0379875,0.923815814919117,1298,100.0
32609,-11716.0833646,-46828.7497757,0.5176622727084385,230,100.0
48749,-13105.5019372,-42739.9637287,1.6352609581970237,1536,100.0
32073,-10864.5266589,-46901.3089472,0.997993476798066,668,100.0
import pandas as pd
df=pd.read_csv("default_list_4.csv", usecols=[3])
df1 = pd.read_csv("list4.csv")
df.to_csv(df1,index=False)
When you do the above, the contents of the existing csv file are deleted, and only the third column of the original csv file is printed in the csv file.
How do I change the default_list_4.csv time in the right column of the botime of an existing file while leaving data from an existing csv file?
Thank you for your cooperation.
Refer to this article, which assumes that both CSV data have the same row order.
Add columns and rows to pandas.DataFrame (assign, append, etc.)
Replace the order of the columns in pandas.DataFrame
However, floating-point numbers may differ in detail, such as 7 decimal places or less, in the conversion of pandas, so I decided to treat them all as strings.
import pandas as pd
dfadd=pd.read_csv("default_list_4.csv", dtype=str, usecols=[3])# All strings
dforg=pd.read_csv("list4.csv", dtype=str)# All strings
dfnew=pd.concat([dforg,dfadd],axis=1)# This line connects both DataFrames horizontally
dfnew=dfnew[['id', 'x', 'y', 'botime', 'detime', 'eldid', 'firev']]# Use this row to rearrange columns
dfnew.to_csv("list4.csv", index=False)
However, in this case, I don't use pandas, which is the requirement of the question, but since the module loading process is light, I think this one will be fine.
import csv
with open('default_list_4.csv') asf:
reader=csv.reader(f)
csvadd = [row[3] for row in reader]
with open('list4.csv') asf:
reader=csv.reader(f)
csvorg = [row for row in reader]
csvnew = [ ]
For org, add in zip (csvorg, csvadd):
org.insert(4, add)
csvnew.append(org)
with open('list4.csv', 'w', newline=') asf:
writer=csv.writer(f)
writer.writerows (csvnew)
Also, it would be better to check the files that have already been processed so that they do not have to be processed again.
© 2024 OneMinuteCode. All rights reserved.