计算结果未添加到现有 csv
Calculated result is not added to the existing csv
我尝试将输出作为新列写入 csv 文件。我可以转换并打印日期和时间,但无法将其写入与“Arrival_time”列中相同的 csv。代码运行正常,没有错误,但 csv 列“到达时间”中没有值。
我哪里错了?
import pandas as pd
df = pd.read_csv ('C:/New.csv',sep=';')
print (df)
import datetime
import csv
#for i in range(0,len(df)):
#row= df.iloc[i]['arrival_unix_seconds']
#a = int(row)
#date = datetime.datetime.utcfromtimestamp(a)
#targetDate = date.strftime("%Y-%m-%d %H:%M:%S")
#print(targetDate)
df['arrival_unix_seconds'] = df.arrival_unix_seconds.apply(lambda x: \
(datetime.datetime.utcfromtimestamp(int(x))).strftime("%Y-%m-%d %H:%M:%S"))
print(df.arrival_unix_seconds)
with open("New.csv", "a") as inputfile:
title = ["id", "space_id", "arrival_unix_seconds", "departure_unix_seconds", "xml_id", "Arrival_time"]
writer = csv.DictWriter(inputfile, delimiter=';', fieldnames = title)
writer.writerow({"Arrival_time": df.arrival_unix_seconds})
由于您已经在使用 pandas 并创建数据框,只需执行以下操作即可计算 Arrival_time
并将其写回现有的 csv:
import pandas as pd
import datetime
df = pd.read_csv('C:/Master thesis/Sensor/parking_operations_berlin_hannover/New.csv', sep=';')
df['Arrival_time'] = df.arrival_unix_seconds.apply(lambda x: (datetime.datetime.utcfromtimestamp(int(x))).strftime("%Y-%m-%d %H:%M:%S"))
df.to_csv('C:/Master thesis/Sensor/parking_operations_berlin_hannover/New.csv', sep=';', index=False)
我尝试将输出作为新列写入 csv 文件。我可以转换并打印日期和时间,但无法将其写入与“Arrival_time”列中相同的 csv。代码运行正常,没有错误,但 csv 列“到达时间”中没有值。
我哪里错了?
import pandas as pd
df = pd.read_csv ('C:/New.csv',sep=';')
print (df)
import datetime
import csv
#for i in range(0,len(df)):
#row= df.iloc[i]['arrival_unix_seconds']
#a = int(row)
#date = datetime.datetime.utcfromtimestamp(a)
#targetDate = date.strftime("%Y-%m-%d %H:%M:%S")
#print(targetDate)
df['arrival_unix_seconds'] = df.arrival_unix_seconds.apply(lambda x: \
(datetime.datetime.utcfromtimestamp(int(x))).strftime("%Y-%m-%d %H:%M:%S"))
print(df.arrival_unix_seconds)
with open("New.csv", "a") as inputfile:
title = ["id", "space_id", "arrival_unix_seconds", "departure_unix_seconds", "xml_id", "Arrival_time"]
writer = csv.DictWriter(inputfile, delimiter=';', fieldnames = title)
writer.writerow({"Arrival_time": df.arrival_unix_seconds})
由于您已经在使用 pandas 并创建数据框,只需执行以下操作即可计算 Arrival_time
并将其写回现有的 csv:
import pandas as pd
import datetime
df = pd.read_csv('C:/Master thesis/Sensor/parking_operations_berlin_hannover/New.csv', sep=';')
df['Arrival_time'] = df.arrival_unix_seconds.apply(lambda x: (datetime.datetime.utcfromtimestamp(int(x))).strftime("%Y-%m-%d %H:%M:%S"))
df.to_csv('C:/Master thesis/Sensor/parking_operations_berlin_hannover/New.csv', sep=';', index=False)