大家好,我有一个程序可以读取 csv 文件并将字符串替换为数字序列,它还有其他列,如日期/时间,它必须仅为所有操作打印日期,该程序运行良好,但我想在该程序中使用Pandas 数据框请有人可以使用此代码并使用所有操作对 Pandas 我对 Pandas 的知识较少我会非常感谢你..谢谢
这是代码
with open(tempFile, 'r',encoding="utf8") as csvfile:
# creating a csv reader object
reader = csv.DictReader(csvfile, delimiter=',')
# next(reader, None)
'''We then restructure the data to be a set of keys with list of values {key_1: [], key_2: []}:'''
data = {}
for row in reader:
# print(row)
for header, value in row.items():
try:
data[header].append(value)
except KeyError:
data[header] = [value]
'''Next we want to give each value in each list a unique identifier.'''
# Loop through all keys
for key in data.keys():
values = data[key]
things = list(sorted(set(values), key=values.index))
for i, x in enumerate(data[key]):
if data[key][i] == "":
data[key][i] = datetime.datetime.now().isoformat()
with open('ram5.csv', "w") as outfile:
writer = csv.writer(outfile)
# Write headers
writer.writerow(data.keys())
# Make one row equal to one value from each list
rows = zip(*data.values())
# Write rows
writer.writerows(rows)
注意:空的日期/时间列被替换为当前日期......所以在这个程序中我得到了所有所需的数据,上面的输出是我编写的程序的输出。 但是想使用 Pandas 数据框编写整个程序..请帮助伙计们,任何帮助都是可观的..thnx
相关分类