【问题标题】:How to select one row in styleframe?如何在样式框中选择一行?
【发布时间】:2020-01-06 22:43:59
【问题描述】:

我想选择前两行来应用样式,但我无法选择它们。

我尝试了很多方法,但都行不通。

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'))

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

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

【问题讨论】:

标签: python excel pandas dataframe styleframe


【解决方案1】:

你可以从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()

将创建

【讨论】:

  • 为什么没有定义“黄色”?
  • 我也想问一下如何指定列的样式以及如何选择列名。
猜你喜欢
  • 2016-04-27
  • 2011-02-18
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 2013-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多