我有下面的xlsx表作为输入,我想用pandas获得输出. 现在我使用openpyxl,但是文件有8k行,200+ col,所以我使用的代码效率不高,运行时间超过20分钟. 示例中所示的文件也有NaN和空单元格,我想只修改不空的单元格. 如代码所示,此代码块必须从第2列和第5行到文件结尾.
# input
Main_col 0 1 2
0 cas1 1_05.04.2024 16:40 A B
1 cas2 5_05.04.2024 16:41 C
2 cas3 4_05.04.2024 17:30 D E
# output
Main_col 0 1 2
0 cas1 1_05.04.2024 16:40 A_05.04.2024 16:40.cas1 1 B_05.04.2024 16:40.cas1 1
1 cas2 5_05.04.2024 16:41 C_05.04.2024 16:41.cas2 5
2 cas3 4_05.04.2024 17:30 D_05.04.2024 17:30.cas3 4 E_05.04.2024 17:30.cas3 4
我使用的代码如下.
for colonn in range(2,ws.max_column+1):
#print("Elaboro colonna: " + str(colonn))
for rig in range(5,ws.max_row+1):
ValoreCell = str(ws.cell(rig,colonn).value)
Valoreheader = str(ws.cell(rig,1).value)
if ValoreCell != None and ValoreCell != " ":
if "_" in ValoreCell:
Valoreheader = Valoreheader.split("_")[0]
#print("Valoreheader " + str(Valoreheader))
Valor = ws.cell(rig,colonn).value
ws.cell(rig,colonn).value = str(Valor) + "." + str(Valoreheader)
我是stackoverflow的新手
使用Pandas 在性能上的重大改进