【发布时间】:2021-06-02 13:21:55
【问题描述】:
我是 Python 新手,在解决这个问题的过程中迷失了方向:我有一个数据框,其中我需要的信息主要分为 2,3 和 4 行的层。每个组在其中一列中具有不同的 ID。我需要创建另一个数据框,其中行组现在是单行,其中信息 unstacked 在更多列中。稍后我可以删除不需要/冗余的列。
我想我需要遍历数据帧行并过滤每个 ID unstacking 行到一个新的数据帧中。我无法从 unstack 或 groupby 函数中获得太多。有没有简单的功能或组合可以完成这项任务?
这是数据框的示例:
2_SH1_G8_D_total;Positions tolerance d [z] ;"";0.000; ;0.060;"";0.032;0.032;53%
12_SH1_G8_D_total;Positions tolerance d [z] ;"";-58.000;"";"";"";---;"";""
12_SH1_G8_D_total;Positions tolerance d [z] ;"";-1324.500;"";"";"";---;"";""
12_SH1_G8_D_total;Positions tolerance d [z] ;"";391.000;"";"";"";390.990;"";""
13_SH1_G8_D_total;Flatness;"";0.000; ;0.020;"";0.004;0.004;20%
14_SH1_G8_D_total;Parallelism tolerance ;"";0.000; ;0.030;"";0.025;0.025;84%
15_SH1_B1_B;Positions tolerance d [x y] ;"";0.000; ;0.200;"";0.022;0.022;11%
15_SH1_B1_B;Positions tolerance d [x y] ;"";265.000;"";"";"";264.993;"";""
15_SH1_B1_B;Positions tolerance d [x y] ;"";1502.800;"";"";"";1502.792;"";""
15_SH1_B1_B;Positions tolerance d [x y] ;"";-391.000;"";"";"";---;"";""
原始数据框包含 4 行信息,但并非总是如此。每个 Id 出现的结束数据框应该只有一行,所有信息都在列中。
到目前为止,在帮助下,我设法运行了这段代码:
with open(path, newline='') as datafile:
data = csv.reader(datafile, delimiter=';')
for row in data:
tmp.append(row)
# Create data table joining data with the same GAT value, GAT is the ID I need
Data = []
Data.append(tmp[0])
GAT = tmp[0][0]
j = 0
counter = 0
for i in range(0,len(tmp)):
if tmp[i][0] == GAT:
counter = counter + 1
if counter == 2:
temp=(tmp[i][5],tmp[i][7],tmp[i][8],tmp[i][9])
else:
temp = (tmp[i][3], tmp[i][7])
Data[j].extend(temp)
else:
Data.append(tmp[i])
GAT = tmp[i][0]
j = j + 1
# for i in range(0,len(Data)):
# print(Data[i])
with open('output.csv', 'w', newline='') as outputfile:
writedata = csv.writer(outputfile, delimiter=';')
for i in range(0, len(Data)):
writedata.writerow(Data[i]);
但并没有真正使用 pandas,这可能会给我更多的数据处理能力。另外,这个open()命令对非ascii字符有问题,我无法解决。
有没有更优雅的方式使用 pandas?
【问题讨论】:
标签: pandas dataframe loops stack