import pandas as pd
from openpyxl import Workbook
from pandas import ExcelWriter import openpyxl
wb = Workbook()
sheet = wb.active
writer = ExcelWriter('path\\test.xlsx')
list1 = [1,1,1,2,3,3,4,5]
list2 = [1,2,2,2,3,4,4,5]
comparison = [i == j for i,j in zip(list1,list2)]
comparison Out[76]: [True, False, False, True, True, False, True, True]
df = pd.DataFrame(list1,list2)
dataframe_spotovi.to_excel(writer,'Jazler spotovi')
writer.save()
값에 따라 첫 번째 열의 행에 색상을 지정하고 싶습니다. 한 색상의 모든 동일한 값 (빨간색 1,1,1, 별도 색상 2, 다른 색상 3,3, 다른 색상 4 등) .). 나는 list2를 만들었으므로 list1의 행이 동일한 값을 갖는지 확인할 수 있습니다 (비교).
나는 다음과 같이 시도했다.
for rows in sheet.iter_rows():
for cell in rows:
for a, b in zip(mirko, darko):
if a == b:
cell.fill = PatternFill(bgColor="FFC7CE", fill_type = "solid")
else:
color = 'FFBB00'
아무것도. 데이터 프레임 스타일러를 시도했지만 스타일러가 반복 할 수 없습니다.
이견있는 사람? 매우 감사.
다음은 일부 데이터에서 데이터 프레임을 만들고 openpyxl
ExcelWriter를 만듭니다 . 그런 다음 Matplotlib의 컬러 맵을 사용하여 다양한 색상을 제공합니다. 각각의 고유 한 값에 대해 빨간색으로 시작하는 컬러 맵의 다음 값을 할당합니다.
import pandas as pd
import openpyxl
from openpyxl.styles import PatternFill
import matplotlib
import matplotlib.cm as cm
import numpy as np
data = [
[1, 'test', 1],
[1, 'test', 2],
[1, 'test', 3],
[2, 'test', 4],
[3, 'test', 5],
[3, 'test', 6],
[4, 'test', 7],
[5, 'test', 8]]
write_path = "output.xlsx"
df = pd.DataFrame(data, columns=["value", "comment", "index"])
unique = np.unique(df['value']).shape[0] + 1
with pd.ExcelWriter(write_path) as writer:
df.to_excel(writer, sheet_name="Sheet1", index=False)
wb = openpyxl.load_workbook(write_path)
ws = wb.get_sheet_by_name("Sheet1")
# Create a color map
tohex = lambda r,g,b,a: '%02X%02X%02X%02X' % (a,r,g,b)
gist_rainbow = cm.gist_rainbow(np.linspace(0, 1, unique))
gist_rainbow = np.array(gist_rainbow * 255, dtype=int)
gist_rainbow = iter([tohex(*gist_rainbow[i,:]) for i in range(unique)])
colours = {}
next_colour = next(gist_rainbow) # get the next colour in the colormap
for cells in ws.iter_rows(min_row=2, min_col=1, max_col=1):
cell = cells[0]
try:
colour = colours[cell.value]
except KeyError:
colours[cell.value] = next_colour
colour = next_colour
next_colour = next(gist_rainbow) # get the next colour in the colormap
cell.fill = PatternFill(start_color=colour, end_color=colour, fill_type='solid')
wb.save(write_path)
그러면 다음과 같은 Excel 스프레드 시트가 제공됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다