将DataFrame导出到Excel并添加样式(背景和颜色)。

huangapple go评论52阅读模式
英文:

dataframe to excel with styling (background and color)

问题

以下是您要翻译的部分:

我有一个如下的数据框,并希望应用两个条件进行样式设置并保存到Excel。

我可以一次执行其中一个条件,但不能同时执行两个条件。

输入: 数据框(2列)和给定的列表(索引号)

条件1: [高亮('background-color: yellow')和红色文本('color:red')但如果第一列的类型不是整数,则文本为蓝色('color:blue')],如果行号在给定的列表中。

条件2: 如果第一列的类型不是整数,则文本为蓝色('color:blue')。

数据框如下:

data_frame={
'column0': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10A, 10: 11B, 11: 12C, 12: 13, 13: 14, 14: 15, 15: 16, 16: 17, 17: 18, 18: 19A, 19: 20B, 20: 21, 21: 22, 22: 23, 23: 24, 24: 25, 25: 26, 26: 27}, 
'column1': {0: 'A', 1: 'V', 2: 'T', 3: 'L', 4: 'G', 5: 'E', 6: 'S', 7: 'G', 8: 'G', 9: 'G', 10: 'L', 11: 'Q', 12: 'T', 13: 'P', 14: 'G', 15: 'G', 16: 'G', 17: 'L', 18: 'S', 19: 'L', 20: 'V', 21: 'C', 22: 'K', 23: 'A', 24: 'S', 25: 'G', 26: 'F'}
}

给定的列表:

given_list=[7,8,9,10,11,12,13,14,15,21,22] ### 数据框的索引号

期望的输出:将DataFrame导出到Excel并添加样式(背景和颜色)。

我尝试过的代码如下:

def highlight(row, row_index):
    background_color = 'background-color: yellow'
    text_color = 'color:red'
    text_color_1 = 'color:blue'
        
    highlited_rows = [f'{text_color}; {background_color}' if row.name in row_index else (f'{text_color_1}' if not isinstance(row[0], int) else '') for _, cell in enumerate(row)]

highlighted_df = df.style.apply(lambda row: highlight(row, row_index), axis=1) 
aligned_df = highlighted_df.set_properties(**{'text-align': 'center'})
aligned_df.to_excel('highlighted_dataframe.xlsx', engine='openpyxl', index=False)

输出:将DataFrame导出到Excel并添加样式(背景和颜色)。

我无法根据两个条件同时着色文本。如何同时应用两个条件,以便获得所需的输出?感谢任何帮助。

英文:

I have a data frame as below and want to apply two conditions for the styling and save into excel.

I could perform either of the condition at a time but not the both simultaneously.

> input: dataframe (2 colums) and a given_list(index numbers)
>
> condition_1: [highlight ('background-color: yellow') and red color
> ('color:red') but if type(column[0])!=int then blue color
> ('color:blue')] if row.numer in the given_list.
>
> condition_2: if type(column[0])!=int then blue color ('color:blue')

data_frame={
'column0': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10A, 10: 11B, 11: 12C, 12: 13, 13: 14, 14: 15, 15: 16, 16: 17, 17: 18, 18: 19A, 19: 20B, 20: 21, 21: 22, 22: 23, 23: 24, 24: 25, 25: 26, 26: 27},
'column1': {0: 'A', 1: 'V', 2: 'T', 3: 'L', 4: 'G', 5: 'E', 6: 'S', 7: 'G', 8: 'G', 9: 'G', 10: 'L', 11: 'Q', 12: 'T', 13: 'P', 14: 'G', 15: 'G', 16: 'G', 17: 'L', 18: 'S', 19: 'L', 20: 'V', 21: 'C', 22: 'K', 23: 'A', 24: 'S', 25: 'G', 26: 'F'}
}

given_list=[7,8,9,10,11,12,13,14,15,21,22] ### the index numbers of the dataframe

desired_output:将DataFrame导出到Excel并添加样式(背景和颜色)。

What I tried:

def highlight(row, row_index):
    # print(type(row[0]))
    background_color = 'background-color: yellow'
    text_color='color:red'
    text_color_1='color:blue'
    
    highlited_rows=[f'{text_color}; {background_color}' if row.name in row_index else (f'{text_color_1}' if not isinstance(row[0], int) else '')for _, cell in enumerate(row)]


highlighted_df =df.style.apply(lambda row: highlight(row, row_index), axis=1) 
aligned_df=highlighted_df.set_properties(**{'text-align': 'center'})
aligned_df.to_excel('highlighted_dataframe.xlsx', engine='openpyxl', index=False)

output=将DataFrame导出到Excel并添加样式(背景和颜色)。

I am not able to color the txt based on the both condition. How to apply both the conditions simultaneously so I can get the desired output?
Any help will be appreciated.

答案1

得分: 2

你可以链式操作样式(请注意正确排序条件),并使用sbuset关键字限制应用于正确单元格/行/列的样式:

given_list = [7, 8, 9, 10, 11, 12, 13, 14, 15, 21, 22]
given_list2 = df.columns[df.iloc[0].map(lambda x: isinstance(x, str))]

(df.style.set_properties(**{'background-color': 'yellow', 'color': 'red'}, subset=given_list)
         .set_properties(**{'color': 'blue'}, subset=pd.IndexSlice['column0', given_list2])
         .set_properties(**{'text-align': 'center'})
         .to_excel('highlighted_dataframe.xlsx', engine='openpyxl', index=False))

将DataFrame导出到Excel并添加样式(背景和颜色)。

英文:

You can chain style operations (take care to sort your conditions in the right order) and use sbuset keyword to limit the styling applied to the correct cells/rows/columns:

given_list = [7, 8, 9, 10, 11, 12, 13, 14, 15, 21, 22]
given_list2 = df.columns[df.iloc[0].map(lambda x: isinstance(x, str))]


(df.style.set_properties(**{'background-color': 'yellow', 'color': 'red'}, subset=given_list)
         .set_properties(**{'color': 'blue'}, subset=pd.IndexSlice['column0', given_list2])
         .set_properties(**{'text-align': 'center'})
         .to_excel('highlighted_dataframe.xlsx', engine='openpyxl', index=False))

将DataFrame导出到Excel并添加样式(背景和颜色)。

答案2

得分: 2

我已经通过以下方式更明确地定义了突出显示和着色条件来解决了这个问题:

df={ 'column0': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10A, 10: 11B, 11: 12C, 12: 13, 13: 14, 14: 15, 15: 16, 16: 17, 17: 18, 18: 19A, 19: 20B, 20: 21, 21: 22, 22: 23, 23: 24, 24: 25, 25: 26, 26: 27}, 'column1': {0: 'A', 1: 'V', 2: 'T', 3: 'L', 4: 'G', 5: 'E', 6: 'S', 7: 'G', 8: 'G', 9: 'G', 10: 'L', 11: 'Q', 12: 'T', 13: 'P', 14: 'G', 15: 'G', 16: 'G', 17: 'L', 18: 'S', 19: 'L', 20: 'V', 21: 'C', 22: 'K', 23: 'A', 24: 'S', 25: 'G', 26: 'F'} }

first_list=[7,8,9,10,11,12,13,14,15,21,22] #行索引

sec_list=df[~df.iloc[:, 0].astype(str).str.isdigit()].index.tolist() ## 创建一个具有column0中非整数值的索引列表

def highlight(row, first_list, sec_list):
    background_color = ''
    text_color=''
    
    if row.name in sec_list and row.name not in first_list:
        text_color = 'color: blue'
        background_color = ''
    
    elif row.name in first_list and row.name not in sec_list: 
        text_color = 'color: red'
        background_color = 'background-color: yellow'
        
    elif row.name in first_list and row.name  in sec_list:
        text_color = 'color: blue'
        background_color = 'background-color: yellow'  
    
    return [f'{text_color}; {background_color}' for _ in row]
    
    
highlighted_df =df.style.apply(lambda row: highlight(row, row_index,sec_list), axis=1) 
aligned_df=highlighted_df.set_properties(**{'text-align': 'center'})
aligned_df.to_excel('highlighted_dataframe.xlsx', engine='openpyxl', index=False)
英文:

I have got it resolved by defining the highlighting and coloring conditions more explicitly as below:

df={ 'column0': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10A, 10: 11B, 11: 12C, 12: 13, 13: 14, 14: 15, 15: 16, 16: 17, 17: 18, 18: 19A, 19: 20B, 20: 21, 21: 22, 22: 23, 23: 24, 24: 25, 25: 26, 26: 27}, 'column1': {0: 'A', 1: 'V', 2: 'T', 3: 'L', 4: 'G', 5: 'E', 6: 'S', 7: 'G', 8: 'G', 9: 'G', 10: 'L', 11: 'Q', 12: 'T', 13: 'P', 14: 'G', 15: 'G', 16: 'G', 17: 'L', 18: 'S', 19: 'L', 20: 'V', 21: 'C', 22: 'K', 23: 'A', 24: 'S', 25: 'G', 26: 'F'} }
first_list=[7,8,9,10,11,12,13,14,15,21,22] #row index
sec_list=df[~df.iloc[:, 0].astype(str).str.isdigit()].index.tolist() ## Makes a list of index having nonint value in column0
def highlight(row, first_list, sec_list):
background_color = ''
text_color=''
if row.name in sec_list and row.name not in first_list:
text_color = 'color: blue'
background_color = ''
elif row.name in first_list and row.name not in sec_list: 
text_color = 'color: red'
background_color = 'background-color: yellow'
elif row.name in first_list and row.name  in sec_list:
text_color = 'color: blue'
background_color = 'background-color: yellow'  
return [f'{text_color}; {background_color}' for _ in row]
highlighted_df =df.style.apply(lambda row: highlight(row, row_index,sec_list), axis=1) 
aligned_df=highlighted_df.set_properties(**{'text-align': 'center'})
aligned_df.to_excel('highlighted_dataframe.xlsx', engine='openpyxl', index=False)

答案3

得分: 0

import pandas as pd
from openpyxl.styles import PatternFill
from openpyxl import Workbook

创建一个数据框架

df = pd.DataFrame({
'水果': ['苹果', '香蕉', '樱桃', '枣'],
'数量': [10, 20, 30, 40],
'价格': [1.5, 2, 3, 4]
})

创建一个工作簿和工作表

wb = Workbook()
ws = wb.active

将数据添加到工作表

for r in dataframe_to_rows(df, index=False, header=True):
ws.append(r)

高亮显示价格大于2的单元格

for row in range(2, ws.max_row + 1):
if ws.cell(row=row, column=3).value > 2:
cell = ws.cell(row=row, column=3)
cell.fill = PatternFill(start_color='FAC090', end_color='FAC090', fill_type='solid')

保存工作簿

wb.save('example.xlsx')

英文:
import pandas as pd
from openpyxl.styles import PatternFill
from openpyxl import Workbook
# create a dataframe
df = pd.DataFrame({
'Fruit': ['Apple', 'Banana', 'Cherry', 'Dates'],
'Quantity': [10, 20, 30, 40],
'Price': [1.5, 2, 3, 4]
})
# create a workbook and worksheet
wb = Workbook()
ws = wb.active
# add data to the worksheet
for r in dataframe_to_rows(df, index=False, header=True):
ws.append(r)
# highlight cells with prices greater than 2
for row in range(2, ws.max_row + 1):
if ws.cell(row=row, column=3).value > 2:
cell = ws.cell(row=row, column=3)
cell.fill = PatternFill(start_color='FAC090', end_color='FAC090', fill_type='solid')
# save the workbook
wb.save('example.xlsx')
# This code will create an Excel file named example.xlsx with the dataframe data, and the cells with prices greater than 2 will be highlighted with an orange color using the PatternFill object.

huangapple
  • 本文由 发表于 2023年5月22日 16:32:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定