gpt4 book ai didi

Python - 面向 CSV 时间的将大量列转换为行

转载 作者:行者123 更新时间:2023-11-30 23:22:50 29 4
gpt4 key购买 nike

我有许多面向“列”的 csv 文件,我需要对其进行预处理才能最终索引它们。

这是面向时间的数据,每个“设备”都有大量的列(最多 128 列),例如:

LDEV_XXXXXX.csv             
Serial number : XXXXX(VSP)
From : 2014/06/04 05:58
To : 2014/06/05 05:58
sampling rate : 1

"No.","time","00:30:00X(X2497-1)","00:30:01X(X2498-1)","00:30:02X(X2499-1)"
"242","2014/06/04 10:00",0,0,0
"243","2014/06/04 10:01",0,0,0
"244","2014/06/04 10:02",9,0,0
"245","2014/06/04 10:03",0,0,0
"246","2014/06/04 10:04",0,0,0
"247","2014/06/04 10:05",0,0,0

我的目标是将数据转置(如果这个术语是正确的)到行中,这样我就能够更有效地操作数据,例如:

"time",device,value
"2014/06/04 10:00","00:30:00X(X2497-1)",0
"2014/06/04 10:00","00:30:01X(X2498-1)",0
"2014/06/04 10:00","00:30:02X(X2499-1)",0
"2014/06/04 10:01","00:30:00X(X2497-1)",0
"2014/06/04 10:01","00:30:01X(X2498-1)",0
"2014/06/04 10:01","00:30:02X(X2499-1)",0
"2014/06/04 10:02","00:30:00X(X2497-1)",9
"2014/06/04 10:02","00:30:01X(X2498-1)",0
"2014/06/04 10:02","00:30:02X(X2499-1)",0

等等...

注意:我已经让原始数据(使用“,”作为分隔符),您会注意到我需要删除没有兴趣的“否”列的前 6 行,但这不是主要目标和难度)

我有一个Python起始代码来转置csv数据,但它并不完全是我所需要的......

import csv
import sys
infile = sys.argv[1]
outfile = sys.argv[2]

with open(infile) as f:
reader = csv.reader(f)
cols = []
for row in reader:
cols.append(row)

with open(outfile, 'wb') as f:
writer = csv.writer(f)
for i in range(len(max(cols, key=len))):
writer.writerow([(c[i] if i<len(c) else '') for c in cols])

请注意,列数是任意的,可以是一些,最多 128,具体取决于文件。

我很确定这是一个常见的需求,但我还找不到执行此操作的确切 python 代码,或者我无法得到...

编辑:

更精确:

每个时间戳行将按设备数量重复,因此文件将有更多行(乘以设备数量),但只有几行(时间戳、设备、值)最终期望的结果已更新:-)

编辑:

我希望能够使用以下脚本:将 argument1 用于 infile,将 argument2 用于 outfile :-)

最佳答案

首先你应该将数据放入你想要的结构中,然后你就可以轻松地将其写出来。此外,对于结构复杂的 csv,使用 DictReader 打开它通常更有用。

from csv import DictReader, DictWriter

with open(csv_path) as f:
table = list(DictReader(f, restval=''))

transformed = []
for row in table:
devices = [d for d in row.viewkeys() - {'time', 'No.'}]
time_rows = [{'time': row['time']} for i in range(len(devices))]
for i, d in enumerate(devices):
time_rows[i].update({'device': d, 'value': row[d]})
transformed += time_rows

这会产生一个类似的列表

[{'device': '00:30:00X(X2497-1)', 'value': '0', 'time': '2014/06/04 10:00'},  
{'device': '00:30:02X(X2499-1)', 'value': '0', 'time': '2014/06/04 10:00'},
{'device': '00:30:01X(X2498-1)', 'value': '0', 'time': '2014/06/04 10:00'},
{'device': '00:30:00X(X2497-1)', 'value': '0', 'time': '2014/06/04 10:01'},
{'device': '00:30:02X(X2499-1)', 'value': '0', 'time': '2014/06/04 10:01'},
{'device': '00:30:01X(X2498-1)', 'value': '0', 'time': '2014/06/04 10:01'},
{'device': '00:30:00X(X2497-1)', 'value': '9', 'time': '2014/06/04 10:02'},
{'device': '00:30:02X(X2499-1)', 'value': '0', 'time': '2014/06/04 10:02'},
{'device': '00:30:01X(X2498-1)', 'value': '0', 'time': '2014/06/04 10:02'},
{'device': '00:30:00X(X2497-1)', 'value': '0', 'time': '2014/06/04 10:03'},
{'device': '00:30:02X(X2499-1)', 'value': '0', 'time': '2014/06/04 10:03'},
{'device': '00:30:01X(X2498-1)', 'value': '0', 'time': '2014/06/04 10:03'},
{'device': '00:30:00X(X2497-1)', 'value': '0', 'time': '2014/06/04 10:04'},
{'device': '00:30:02X(X2499-1)', 'value': '0', 'time': '2014/06/04 10:04'},
{'device': '00:30:01X(X2498-1)', 'value': '0', 'time': '2014/06/04 10:04'},
{'device': '00:30:00X(X2497-1)', 'value': '0', 'time': '2014/06/04 10:05'},
{'device': '00:30:02X(X2499-1)', 'value': '0', 'time': '2014/06/04 10:05'},
{'device': '00:30:01X(X2498-1)', 'value': '0', 'time': '2014/06/04 10:05'}]

这正是我们想要的。然后,要将其写回,您可以使用 DictWriter。

# you might sort transformed here so that it gets written out in whatever order you like

column_names = ['time', 'device', 'value']
with open(out_path, 'w') as f:
writer = DictWriter(f, column_names)
writer.writeheader()
writer.writerows(transformed)

关于Python - 面向 CSV 时间的将大量列转换为行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24295855/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com