AttributeError: 'set' object has no attribute 'keys'
AttributeError: 'set' object has no attribute 'keys'
我正在尝试将 Unix 秒数从 csv 文件转换为日期和时间,并尝试将输出作为新列写入 csv 文件。我可以转换并打印日期和时间,但我无法将其写入与新列相同的 csv。
AttributeError: 'set' object has no attribute 'keys'
我尝试用不同的方式写作但卡住了。我做错了什么?
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)
#targetDate.to_csv("b.csv", sep=';', index=False)
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: targetDate"})
示例数据
id;space_id;arrival_unix_seconds;departure_unix_seconds;xml_id;Arrival_time
2950;72729;1619808731;1619809039;22
320;16639;1612627087;1612628433;76
82;3408;1609751088;1609751723;7
尝试apply
它比逐行迭代快得多
df['arrival_unix_seconds'] = df.arrival_unix_seconds.apply(lambda x: \
(datetime.datetime.utcfromtimestamp(int(x))).strftime("%Y-%m-%d %H:%M:%S"))
# Save it like this
pd.to_csv('NEW.csv', index=False)
而不是这个
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)
#targetDate.to_csv("b.csv", sep=';', index=False)
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: targetdata"})
我正在尝试将 Unix 秒数从 csv 文件转换为日期和时间,并尝试将输出作为新列写入 csv 文件。我可以转换并打印日期和时间,但我无法将其写入与新列相同的 csv。
AttributeError: 'set' object has no attribute 'keys'
我尝试用不同的方式写作但卡住了。我做错了什么?
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)
#targetDate.to_csv("b.csv", sep=';', index=False)
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: targetDate"})
示例数据
id;space_id;arrival_unix_seconds;departure_unix_seconds;xml_id;Arrival_time 2950;72729;1619808731;1619809039;22
320;16639;1612627087;1612628433;76
82;3408;1609751088;1609751723;7
尝试apply
它比逐行迭代快得多
df['arrival_unix_seconds'] = df.arrival_unix_seconds.apply(lambda x: \
(datetime.datetime.utcfromtimestamp(int(x))).strftime("%Y-%m-%d %H:%M:%S"))
# Save it like this
pd.to_csv('NEW.csv', index=False)
而不是这个
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)
#targetDate.to_csv("b.csv", sep=';', index=False)
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: targetdata"})