gpt4 book ai didi

python - 使用 python pandas style.where (或其他方式)为不匹配的两列中的特定单元格着色并导出到 Excel

转载 作者:行者123 更新时间:2023-12-04 19:55:57 25 4
gpt4 key购买 nike

我希望为两列不匹配的特定单元格着色,但想将其与 python pandas style.where 一起使用并使用 openpyxl 在 Excel 中导出。

到目前为止我的代码:

df = pd.DataFrame({
'config_dummy1': ["dummytext"] * 100,
'config_size_x': ["textstring"] * 100,
'config_size_y': ["textstring"] * 100,
'config_dummy2': ["dummytext"] * 100
})
df.at[50, 'config_size_x'] = "xandydontmatch"
df.at[99, 'config_size_y'] = "xandydontmatch"
print(df)
df.style.where(
df['config_size_x'] != df['config_size_y'],
'color: #ffffff; background-color: #ba3018',
other=''
).to_excel('styled.xlsx', engine='openpyxl')

我被卡住了,因为它产生了一个错误:

Traceback (most recent call last):
File "python-match-csv.py", line 205, in <module>
main2()
File "python-match-csv.py", line 131, in main2
).to_excel('styled.xlsx', engine='openpyxl')
File "F:\Python36\lib\site-packages\pandas\io\formats\style.py", line 175, in to_excel
engine=engine)
File "F:\Python36\lib\site-packages\pandas\io\formats\excel.py", line 652, in write
freeze_panes=freeze_panes)
File "F:\Python36\lib\site-packages\pandas\io\excel.py", line 1390, in write_cells
for cell in cells:
File "F:\Python36\lib\site-packages\pandas\io\formats\excel.py", line 617, in get_formatted_cells
self._format_body()):
File "F:\Python36\lib\site-packages\pandas\io\formats\excel.py", line 529, in _format_regular_rows
for cell in self._generate_body(coloffset):
File "F:\Python36\lib\site-packages\pandas\io\formats\excel.py", line 601, in _generate_body
styles = self.styler._compute().ctx
File "F:\Python36\lib\site-packages\pandas\io\formats\style.py", line 514, in _compute
r = func(self)(*args, **kwargs)
File "F:\Python36\lib\site-packages\pandas\io\formats\style.py", line 604, in _applymap
result = self.data.loc[subset].applymap(func)
File "F:\Python36\lib\site-packages\pandas\core\frame.py", line 6072, in applymap
return self.apply(infer)
File "F:\Python36\lib\site-packages\pandas\core\frame.py", line 6014, in apply
return op.get_result()
File "F:\Python36\lib\site-packages\pandas\core\apply.py", line 318, in get_result
return super(FrameRowApply, self).get_result()
File "F:\Python36\lib\site-packages\pandas\core\apply.py", line 142, in get_result
return self.apply_standard()
File "F:\Python36\lib\site-packages\pandas\core\apply.py", line 248, in apply_standard
self.apply_series_generator()
File "F:\Python36\lib\site-packages\pandas\core\apply.py", line 277, in apply_series_generator
results[i] = self.f(v)
File "F:\Python36\lib\site-packages\pandas\core\frame.py", line 6070, in infer
return lib.map_infer(x.astype(object).values, func)
File "pandas/_libs/src\inference.pyx", line 1472, in pandas._libs.lib.map_infer
File "F:\Python36\lib\site-packages\pandas\io\formats\style.py", line 671, in <lambda>
return self.applymap(lambda val: value if cond(val) else other,
TypeError: ("'Series' object is not callable", 'occurred at index config_dummy1')

类型错误:(“‘Series’对象不可调用”,‘发生在索引 config_dummy1’

我愿意接受 .where() 以外的建议,我也尝试使用 .apply() 来做到这一点,但失败了。

注意:列索引位置不固定,可以是config_size_x、config_dummy1、config_dummy2、config_size_y或任何其他组合

注释 2:如果重要,请使用 Windows 和 Python 3.6

最佳答案

由于这个问题被标记为styleframe:

from StyleFrame import StyleFrame, Styler

df = pd.DataFrame({'a': [1, 2], 'b': [1, 3]})

sf = StyleFrame(df)
sf.apply_style_by_indexes(sf[sf['a'] != sf['b']], styler_obj=Styler(bg_color='red'))
sf.to_excel('test.xlsx').save()

将产生

enter image description here

如果您只想为不匹配行的子集着色,您可以简单地使用 cols_to_style 参数:

sf.apply_style_by_indexes(sf[sf['a'] != sf['b']], styler_obj=Styler(bg_color='red'),
cols_to_style=['a', 'b'])

enter image description here

关于python - 使用 python pandas style.where (或其他方式)为不匹配的两列中的特定单元格着色并导出到 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51175491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com