[英]Python Excel Reformatting
最近我一直在使用 excel 工作表,我需要以某种方式对其进行格式化(如下所示)。 以下是我正在使用的 excel 表(对于已编辑的内容表示歉意,某些信息很敏感,也为图像道歉,我对 Stack Overflow 还很陌生,不知道如何添加 excel 数据):
以上是我目前使用的格式,但是我需要将数据转换为以下格式:
如您所见,我需要 go 的数据从 10 行到每个唯一 LBREFID 的 1 行。 我已经尝试对数据使用不同的 Pandas 函数,例如 .tolist() 和 .pivot() ,但这会导致数据与所需格式不符。 这是一个有趣的问题,不幸的是,我没有时间解决。 预先感谢您的帮助。
import pandas._testing as tm
import pandas as pd
import numpy as np
tests = ["BMCELL", "PLASMA", "NEOPLASMABM", "NEOPLASMATBM", "CD138", "CD56", "CYCLIND1", "KAPPA", "LAMBDA", "NEOPLASMA"]
df = load_workbook(filename='GregFileComparison\\NovemberData.xlsx')
sheet = df['Sheet1']
i = 0
a = 3
e = 11
while (i <= 227):
for row in sheet['A' + str(a) + ':E' + str(e)]:
for cell in row:
cell.value = None
for row in sheet['I' + str(a) + ':J' + str(e)]:
for cell in row:
cell.value = None
for row in sheet['M' + str(a) + ':N' + str(e)]:
for cell in row:
cell.value = None
a += 10
e += 10
i += 1
sheet.delete_cols(12)
sheet.delete_cols(7)
i = 11
while (i <= 19):
sheet.insert_cols(i)
i += 1
counter = 10
i = 0
while (i <= 9):
sheet.cell(row=1, column=counter).value = tests[i]
counter += 1
i += 1
j = 0
i = 3
counter = 1
while (j <= 250):
while (counter <= 9):
sheet.move_range("J" + str(i), rows=-(counter), cols=counter)
i += 1
counter += 1
j += 1
counter = 0
sheet.delete_cols(6)
sheet.delete_cols(6)
df.save('output.xlsx')```
I found that hardcoding the transformations on the excel sheet worked best.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.