我希望得到指导,从样式化的 pandas 数据帧的输出中删除前缀“none_”。 前缀 none_ 用于标识单词,其背景颜色需要更改为灰色。 识别出的单词的背景颜色已更改为灰色后。 我想从单词中删除前缀 none_。
这是数据和代码。
文件1 abcdefg 12345 无_abd abcdrtyu
文件2 abcdefg 无_acd 12345 abcdrtyu
文件3 abcdefg 12345 abcdrtyu 无_abcd
File1、File2 和 File3 是列标题,它们下面的行是 panda 数据框中的行。 对于具有以前缀“none_”开头的单词的单元格,我想将单元格的背景颜色更改为浅灰色。 将单词的背景颜色更改为灰色后,我想删除前缀“none_”。 并将数据输出到 html 或 xlsx 文档。
这是代码:
import pandas as pd
# Create a pandas DataFrame from the data
df = pd.DataFrame(data)
df.style
def style_highlight_none(v, props=''):
return props if v.startswith('none_') else None
s2 = df.style.map(style_highlight_none, props='background-color:lightgrey;')
s2.to_html('output_wa.html', index=False, escape=False)
这样编写的代码运行时不会抛出任何错误。 但是,前缀 none_ 仍然存在于输出中。
当添加这行代码时,如下所示:
s2 = s2.apply(lambda x: x.str.replace('none_', ''))
import pandas as pd
# Create a pandas DataFrame from the data
df = pd.DataFrame(data)
df.style
def style_highlight_none(v, props=''):
return props if v.startswith('none_') else None
s2 = df.style.map(style_highlight_none, props='background-color:lightgrey;')
# Remove the prefix 'none_' from words containing it
s2 = s2.apply(lambda x: x.str.replace('none_', ''))
s2.to_html('output.html', index=False, escape=False)
代码返回各种错误。
回溯(最近一次调用最后一次): 文件“/home/mydir/script/./whatsapp_pathlib_html_output.py”,第 111 行,位于 s2.to_html('output_wa.html', index=False, escape=False) 文件“/home/mydir/.local/lib/python3.10/site-packages/pandas/io/formats/style.py”,第 1345 行,to_html html = obj._render_html( 文件“/home/mydir/.local/lib/python3.10/site-packages/pandas/io/formats/style_render.py”,第 204 行,在 _render_html 中 d = self._render(sparse_index,sparse_columns,max_rows,max_cols,“ ”) 文件“/home/mydir/.local/lib/python3.10/site-packages/pandas/io/formats/style_render.py”,第 161 行,在 _render 中 self._compute() 文件“/home/mydir/.local/lib/python3.10/site-packages/pandas/io/formats/style_render.py”,第 256 行,在 _compute 中 r = func(self)(*args, **kwargs) 文件“/home/mydir/.local/lib/python3.10/site-packages/pandas/io/formats/style.py”,第 1752 行,在 _apply 中 self._update_ctx(结果) 文件“/home/mydir/.local/lib/python3.10/site-packages/pandas/io/formats/style.py”,第 1560 行,位于 _update_ctx css_list = Maybe_convert_css_to_tuples(c) 文件“/home/mydir/.local/lib/python3.10/site-packages/pandas/io/formats/style_render.py”,第 1925 行,在 Maybe_convert_css_to_tuples 中 引发值错误( ValueError:作为字符串提供的样式必须遵循 CSS 规则格式,例如“attr: val;”。给出了“1245-67ABC”。
要实现以浅灰色背景颜色突出显示带有“none_”前缀的单词的单元格,然后删除“none_”前缀的目标,您可以使用
pd.DataFrame.style.applymap
来设置样式,使用 pd.DataFrame.replace
来删除前缀。 。这是代码的更正版本:
import pandas as pd
from pandas.io.formats.style import Styler
# Sample data
data = {
'File1': ['abcdefg', '12345', 'none_abd', 'abcdrtyu'],
'File2': ['abcdefg', 'none_acd', '12345', 'abcdrtyu'],
'File3': ['abcdefg', '12345', 'abcdrtyu', 'none_abcd']
}
# Create a pandas DataFrame from the data
df = pd.DataFrame(data)
# Function to style the cells with "none_" prefix
def style_highlight_none(val):
color = 'lightgrey' if str(val).startswith('none_') else ''
return f'background-color: {color}'
# Apply the styling
styled_df = df.style.applymap(style_highlight_none)
# Remove the prefix 'none_'
df = df.replace(to_replace=r'^none_', value='', regex=True)
# Save the styled DataFrame to an HTML file
styled_df.to_html('output_styled.html', index=False, escape=False)
# Save the modified DataFrame (without the prefix) to an HTML file
df.to_html('output_clean.html', index=False, escape=False)
# Optionally, save both DataFrames to Excel
with pd.ExcelWriter('output.xlsx') as writer:
styled_df.to_excel(writer, sheet_name='Styled', index=False)
df.to_excel(writer, sheet_name='Clean', index=False)
style_highlight_none
函数定义为将浅灰色背景颜色应用于以“none_”开头的单元格。applymap
用于将style_highlight_none
函数应用到DataFrame中的每个单元格。replace
方法用于从DataFrame中删除“none_”前缀。output_styled.html
包含带有原始数据(前缀完整)的样式化 DataFrame。output_clean.html
包含不带“none_”前缀的 DataFrame。output.xlsx
。通过将样式和前缀删除分为两个步骤并相应保存,您可以避免错误并确保输出满足您的要求。