xlsxwriter pandas 框架:如果列中有空白单元格,则突出显示行
xlsxwriter pandas frame: to highlight rows if there are blank cells within a column
我有一个 pandas 框架,其 T 列有一些空白单元格。我想突出显示任何包含空白单元格的行
我一直在尝试使用 .format,但它只突出显示空白单元格而不是整行。
worksheet.conditional_format('A1:T18', {'type':'no_blank'
'format':green_fmt}
)
预期:整行以浅绿色突出显示
实际结果:只有空白单元格突出显示
如果空白值是缺失值,请使用 pandas styles 和自定义函数:
df = pd.DataFrame({'T':[np.nan, np.nan, 1, 5],
'A':range(4),
'B':list('abcd')})
print (df)
T A B
0 NaN 0 a
1 NaN 1 b
2 1.0 2 c
3 5.0 3 d
def highlight(x):
c = 'background-color: lime'
df1 = pd.DataFrame('', index=x.index, columns=x.columns)
m = x.isna().any(axis=1)
df1 = df1.mask(m, c)
return df1
df.style.apply(highlight, axis=None).to_excel('styled.xlsx', engine='openpyxl', index=False)
1. 构建一个函数,在找到 NaN 时突出显示行。
2. dataframe.style.apply(function_name, 轴=1)
# Function to color entire row
def color(row):
if row.isnull().values.any() == True:
return ['background-color: red'] * len(row)
return [''] * len(row)
# Create a dataframe
data = pd.DataFrame({"col1":col1, "col2":col2, "col3":col3})
# Empty values
col1[3], col2[0] = None, None
# Apply the function
data.style.apply(color, axis=1)
这对我有用:
import pandas as pd
import numpy as np
import xlsxwriter
# Create a test dataframe (borrowed by jezrael)
df = pd.DataFrame({'T':[np.nan, np.nan, 1, 5],
'A':range(4),
'B':list('abcd')})
# Create a Pandas Excel writer using XlsxWriter as the engine
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object
df.to_excel(writer, sheet_name='Sheet1', index=False)
# Get the xlsxwriter workbook and worksheet objects
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Define the format for the row
cell_format = workbook.add_format({'bg_color': 'yellow'})
# Grab the index numbers of the rows where specified column has blank cells (in this case column T)
rows_with_blank_cells = df.index[pd.isnull(df['T'])]
# For loops to apply the format only to the rows which have blank cells
for col in range(0,df.shape[1]): # iterate through every column of the df
for row in rows_with_blank_cells:
if pd.isnull(df.iloc[row,col]): # if cell is blank you ll get error, that's why write None value
worksheet.write(row+1, col, None, cell_format)
else:
worksheet.write(row+1, col, df.iloc[row,col], cell_format)
# Finally output the file
writer.save()
我有一个 pandas 框架,其 T 列有一些空白单元格。我想突出显示任何包含空白单元格的行
我一直在尝试使用 .format,但它只突出显示空白单元格而不是整行。
worksheet.conditional_format('A1:T18', {'type':'no_blank'
'format':green_fmt}
)
预期:整行以浅绿色突出显示 实际结果:只有空白单元格突出显示
如果空白值是缺失值,请使用 pandas styles 和自定义函数:
df = pd.DataFrame({'T':[np.nan, np.nan, 1, 5],
'A':range(4),
'B':list('abcd')})
print (df)
T A B
0 NaN 0 a
1 NaN 1 b
2 1.0 2 c
3 5.0 3 d
def highlight(x):
c = 'background-color: lime'
df1 = pd.DataFrame('', index=x.index, columns=x.columns)
m = x.isna().any(axis=1)
df1 = df1.mask(m, c)
return df1
df.style.apply(highlight, axis=None).to_excel('styled.xlsx', engine='openpyxl', index=False)
1. 构建一个函数,在找到 NaN 时突出显示行。
2. dataframe.style.apply(function_name, 轴=1)
# Function to color entire row
def color(row):
if row.isnull().values.any() == True:
return ['background-color: red'] * len(row)
return [''] * len(row)
# Create a dataframe
data = pd.DataFrame({"col1":col1, "col2":col2, "col3":col3})
# Empty values
col1[3], col2[0] = None, None
# Apply the function
data.style.apply(color, axis=1)
这对我有用:
import pandas as pd
import numpy as np
import xlsxwriter
# Create a test dataframe (borrowed by jezrael)
df = pd.DataFrame({'T':[np.nan, np.nan, 1, 5],
'A':range(4),
'B':list('abcd')})
# Create a Pandas Excel writer using XlsxWriter as the engine
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object
df.to_excel(writer, sheet_name='Sheet1', index=False)
# Get the xlsxwriter workbook and worksheet objects
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Define the format for the row
cell_format = workbook.add_format({'bg_color': 'yellow'})
# Grab the index numbers of the rows where specified column has blank cells (in this case column T)
rows_with_blank_cells = df.index[pd.isnull(df['T'])]
# For loops to apply the format only to the rows which have blank cells
for col in range(0,df.shape[1]): # iterate through every column of the df
for row in rows_with_blank_cells:
if pd.isnull(df.iloc[row,col]): # if cell is blank you ll get error, that's why write None value
worksheet.write(row+1, col, None, cell_format)
else:
worksheet.write(row+1, col, df.iloc[row,col], cell_format)
# Finally output the file
writer.save()