pandas-styles

Export styled pandas data frame to excel

元气小坏坏 提交于 2020-06-27 15:21:56
问题 I'm trying to export a stylish data frame to an exel file using the scrpit bellow import pandas as pd import numpy as np np.random.seed(24) df = pd.DataFrame({'A': np.linspace(1, 10, 10)}) df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],axis=1) df.iloc[0, 2] = np.nan def highlight_greater(x): r = 'red' g = 'gray' m1 = x['B'] > x['C'] m2 = x['D'] > x['E'] df1 = pd.DataFrame('background-color: ', index=x.index, columns=x.columns) #rewrite values by boolean masks

Pandas style doesn't work with Google colab

瘦欲@ 提交于 2020-06-27 10:22:27
问题 I'm facing a strange behavior with Google colab and pandas style. When applying a style to a dataframe on google colab, some of the basic styling is messed up: the table becomes smaller and more condensed, the highlight of every other row disappears, and the hover highlight of rows stops working. I'm attaching a side-by-side picture of two screenshots: one from a regular Jupiter notebook, in which things are working fine, and another one from Google colab - in which styling messes up things.

Pandas style.bar color based on condition?

ⅰ亾dé卋堺 提交于 2020-06-26 08:44:51
问题 How can I render a Pandas df where one of the columns' style.bar.color property is computed based on some condition? Example: df.style.bar(subset=['before', 'after'], color='#ff781c', vmin=0.0, vmax=1.0) Instead of having both columns highlight with #ff781c , I'd like one of the columns ( df['before'] ) to remain that same constant color, and the other column ( df['after'] ) to be computed as: def compute_color(row): if row['after'] >= row['before']: return 'red' else: return 'green 回答1: One

Pandas highlight rows based on index name

人走茶凉 提交于 2020-02-24 15:13:22
问题 I have been struggling with how to style highlight pandas rows based on index names. I know how to highlight selected rows but when I have to highlight based on the index, the code is not working. Setup df = pd.DataFrame({'key': list('ABCD'), 'value': range(4)}) print(df) key value 0 A 0 1 B 1 2 C 2 3 D 3 Highlight rows when key has value 'B' or 'D' # this works df.style.apply(lambda x: ['background: lightgreen' if (x.key == 'B' or x.key == 'D') else '' for i in x], axis=1) Highlight rows

Pandas highlight rows based on index name

烂漫一生 提交于 2020-02-24 15:12:14
问题 I have been struggling with how to style highlight pandas rows based on index names. I know how to highlight selected rows but when I have to highlight based on the index, the code is not working. Setup df = pd.DataFrame({'key': list('ABCD'), 'value': range(4)}) print(df) key value 0 A 0 1 B 1 2 C 2 3 D 3 Highlight rows when key has value 'B' or 'D' # this works df.style.apply(lambda x: ['background: lightgreen' if (x.key == 'B' or x.key == 'D') else '' for i in x], axis=1) Highlight rows

Pandas highlight rows based on index name

你。 提交于 2020-02-24 15:10:18
问题 I have been struggling with how to style highlight pandas rows based on index names. I know how to highlight selected rows but when I have to highlight based on the index, the code is not working. Setup df = pd.DataFrame({'key': list('ABCD'), 'value': range(4)}) print(df) key value 0 A 0 1 B 1 2 C 2 3 D 3 Highlight rows when key has value 'B' or 'D' # this works df.style.apply(lambda x: ['background: lightgreen' if (x.key == 'B' or x.key == 'D') else '' for i in x], axis=1) Highlight rows

Combining pandas “data frame.style” objects and outputting to html

会有一股神秘感。 提交于 2020-01-06 08:06:15
问题 I would like to combine pandas df.style objects that use the following methods - df.style.background_gradient and df.style.bar and export the result to html. I'm successful when I have separate objects. Here is an example data frame: df = pd.DataFrame(np.random.randn(15).reshape(5, 3)) I can then pass this to the different style methods: # Use method "background_gradient" styled_df_a = df.style.background_gradient(subset=[0, 1], low=0, high=0.5, cmap="YlGnBu") # Use method "bar" styled_df_b =

Python Pandas: Style column header

只谈情不闲聊 提交于 2019-12-01 23:04:10
问题 I am using pandas styler to give some columns a background color, based on the name of the column header. While this works as intended, the background color of the column header doesn't change. Here is the part in my script where thy style is applied: def highlight_col(x): if x.name in added_columns: return ['background-color: #67c5a4']*x.shape[0] elif x.name in dropped_columns: return ['background-color: #ff9090']*x.shape[0] else: return ['background-color: None']*x.shape[0] old = old.style