简体   繁体   English

Python Excel 重新格式化

[英]Python Excel Reformatting

Recently I have been working with an excel sheet for work and I need to format it in a certain way (shown below).最近我一直在使用 excel 工作表,我需要以某种方式对其进行格式化(如下所示)。 The following is the excel sheet I'm working with (apologies for the REDACTED, some of the information is sensitive, also apologize for the image, I am fairly new to Stack Overflow and do not know how to add excel data):以下是我正在使用的 excel 表(对于已编辑的内容表示歉意,某些信息很敏感,也为图像道歉,我对 Stack Overflow 还很陌生,不知道如何添加 excel 数据):

当前格式

Above is the format that I currently am using, but I need to convert the data to the following format:以上是我目前使用的格式,但是我需要将数据转换为以下格式:

所需格式

As you can see I need the data to go from 10 lines, down to 1 line per unique LBREFID.如您所见,我需要 go 的数据从 10 行到每个唯一 LBREFID 的 1 行。 I have already tried to use different Pandas functions such as.tolist() and.pivot() for the data, but that would result in data that does not resemble the desired format.我已经尝试对数据使用不同的 Pandas 函数,例如 .tolist() 和 .pivot() ,但这会导致数据与所需格式不符。 This is an interesting problem that I, unfortunately, do not have the time to solve.这是一个有趣的问题,不幸的是,我没有时间解决。 Thank you in advance for your help.预先感谢您的帮助。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM