0
votes

I want to select the first two rows to apply styles, but I cannot select them.

I have tried many methods, but all cannot work.

SH1.apply_style_by_indexes(indexes_to_style=SH1[SH1[0]], styler_obj=Styler(bold=True))
SH1.apply_style_by_indexes(indexes_to_style=SH1[SH1.loc[0]], styler_obj=Styler(bold=True))
SH1.style_alternate_rows(SH1[1], styles=Styler(font_color='green'))

File "C:\Users\dell\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\indexes\base.py", line 2897, in get_loc return self._engine.get_loc(key) File "pandas_libs\index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas_libs\hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 1

SH1.style_alternate_rows(SH1[1], styles=Styler(font_color='green')) File "C:\Users\dell\AppData\Roaming\Python\Python37\site-packages\StyleFrame\style_frame.py", line 109, in getitem return Series(self.data_df.getitem(item)) File "C:\Users\dell\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 2980, in getitem indexer = self.columns.get_loc(key) File "C:\Users\dell\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\indexes\base.py", line 2899, in get_loc return self._engine.get_loc(self._maybe_cast_indexer(key)) File "pandas_libs\index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas_libs\hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 1

1
Comments are not for extended discussion; this conversation has been moved to chat.Samuel Liew♦

1 Answers

1
votes

You can grab the first and second indexes from sf.index:

sf = StyleFrame({'a': ['a', 'b', 'c', 'd']})
yellow = Styler(bg_color='yellow')
blue = Styler(bg_color='blue')

sf.apply_style_by_indexes(sf.index[0], yellow)
sf.apply_style_by_indexes(sf.index[1], blue)
sf.to_excel().save()

Will create

enter image description here