[英]Python - Reading in data from csv and then writing to new csv with data and file number from loop
My objective is to:我的目标是:
import pandas as pd import csv from glob import glob从全局导入 pandas 作为 pd 导入 csv 从全局导入全局
filenames = glob("C:/Users/Z*.csv")文件名 = glob("C:/Users/Z*.csv")
for f in filenames:
df = pd.read_csv(f, sep=',',skiprows=24)
df.columns=['sample','Time','ms','Temperature']
df=df.astype(str)
df["Temperature"] = df["Temperature"].str.replace('\+ ', '').str.replace(' ', '').astype(float)
# I need to add the column, "File #" to the Dataframe
df.to_csv('my_csv.csv', mode='a', columns=[['Temperature','File #']] header=False)
# Is df.to_csv is the best option here?
# Then, I need to iterate the file # for the next loop.
please find my suggestions below - hope this helps!请在下面找到我的建议-希望这会有所帮助!
for i, f in enumerate(filenames):
df = pd.read_csv(f, sep=',',skiprows=24)
df.columns=['sample','Time','ms','Temperature']
df=df.astype(str)
df["Temperature"] = df["Temperature"].str.replace('\+ ', '').str.replace(' ', '').astype(float)
# I need to add the column, "File #" to the Dataframe
df['File #'] = i
df.to_csv('my_csv.csv', mode='a', columns=[['Temperature','File #']] header=False)
# Is df.to_csv is the best option here? - probably yes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.