0

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

bharatk
  • 4,202
  • 5
  • 16
  • 30
Trinidad
  • 211
  • 2
  • 14
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/198929/discussion-on-question-by-trinidad-how-to-select-one-row-in-styleframe). – Samuel Liew Sep 04 '19 at 10:05

1 Answers1

1

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

DeepSpace
  • 78,697
  • 11
  • 109
  • 154