반응형
판다와 함께 세포를 훌륭하게 색칠하기
여기 도움이 좀 필요해요.그래서 저는 이런 게 있어요.
import pandas as pd
path = '/Users/arronteb/Desktop/excel/ejemplo.xlsx'
xlsx = pd.ExcelFile(path)
df = pd.read_excel(xlsx,'Sheet1')
df['is_duplicated'] = df.duplicated('#CSR')
df_nodup = df.loc[df['is_duplicated'] == False]
df_nodup.to_excel('ejemplo.xlsx', encoding='utf-8')
그래서 기본적으로 이 프로그램은 로드됩니다.ejemplo.xlsx
(ejemplo는 스페인어로 예이며 파일 이름뿐입니다)df
(a)DataFrame
)에서 특정 열에 중복된 값이 있는지 확인합니다.그러면 중복 항목이 삭제되고 파일이 다시 저장됩니다.그 부분은 제대로 작동합니다.문제는 중복된 것을 제거하는 대신 노란색과 같은 다른 색으로 포함된 셀을 강조 표시해야 한다는 것입니다.
강조 표시 기능을 만들 수 있습니다...
def highlight_cells():
# provide your criteria for highlighting the cells here
return ['background-color: yellow']
그런 다음 강조 표시 기능을 데이터 프레임에 적용합니다.
df.style.apply(highlight_cells)
저도 방금 같은 문제가 있었는데 이번 주에 해결했습니다.문제는 올바르게 작동하는 것을 발견한 온라인 코드를 얻기 위해 포함 항목이 제대로 작동하지 않는 것이었습니다.
글꼴 색을 바꾸는 것이 아니라 배경 색을 바꾸는 것을 의미한다고 생각합니다.제가 틀렸다면 당신의 요청을 명확히 해주세요.
내 솔루션은 특정 라이브러리에 연결되어 있습니다.openpyxl
#### This import section is where my mistake was at
#### This works for me
import openpyxl ### Excel files
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
from openpyxl.styles import Fill, Color
from openpyxl.styles import Style
from openpyxl.styles.colors import RED
from openpyxl.styles.colors import GREEN
str_xls_PathFileCurrent = str_xls_FileName
### Opens Excel Document
var_xls_FileOpen = openpyxl.load_workbook(str_xls_PathFileCurrent)
### Opens up the Excel worksheet
var_xls_TabName = var_xls_FileOpen.worksheets[0]
### Put the spreadsheet tab names into an array
ary_xls_SheetNames = var_xls_FileOpen.get_sheet_names()
### Open the sheet in the file you working on
var_xls_TabSheet = var_xls_FileOpen.get_sheet_by_name(ary_xls_SheetNames[0])
xls_cell = var_xls_TabSheet['d10']
#### Changes the cell background color
xls_cell.style = Style(fill=PatternFill(patternType='solid'
, fgColor=Color('C4C4C4'))) ### Changes background color
#### Changes the fonts (does not use style)
xls_cell.font = xls_cell.font.copy(color = 'FFFF0000') ### Works (Changes to red font text)
xls_cell.font = xls_cell.font.copy(bold = True) ### Works (Changes to bold font)
xls_cell.font = xls_cell.font.copy(italic= True) ### Works (Changes to Italic Text)
xls_cell.font = xls_cell.font.copy(size = 34) ### Works (Changes Size)
언급URL : https://stackoverflow.com/questions/39299509/coloring-cells-in-excel-with-pandas
반응형
'sourcetip' 카테고리의 다른 글
문자열 배열을 문자열로 변환 (0) | 2023.05.23 |
---|---|
오브젝티브-C의 "@synchronized"에 해당하는 스위프트는 무엇입니까? (0) | 2023.05.23 |
이미지 URL을 system.drawing.image로 변환하려면 어떻게 해야 합니까? (0) | 2023.05.23 |
쉬운 인터뷰 질문이 어려워졌습니다: 1번이 주어졌습니다.100, 정확하게 k가 누락된 경우 결측 번호 찾기 (0) | 2023.05.23 |
python에서 목록이 항목을 공유하는지 테스트합니다. (0) | 2023.05.23 |