# Pandas Dataframe Styles are not working with Jupyter Notebook

**URL:** <https://discuss.python.org/t/pandas-dataframe-styles-are-not-working-with-jupyter-notebook/3131>\
**Category:** Python Help\
**Created:** [February 5, 2020, 5:54pm UTC](https://discuss.python.org/t/pandas-dataframe-styles-are-not-working-with-jupyter-notebook/3131 "2020-02-05T17:54:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dinesh1393](https://avatars.discourse-cdn.com/v4/letter/d/b487fb/32.png) [@dinesh1393](https://discuss.python.org/u/dinesh1393)\
**Post date:** [February 5, 2020, 5:54pm UTC](https://discuss.python.org/t/pandas-dataframe-styles-are-not-working-with-jupyter-notebook/3131/1 "2020-02-05T17:54:13Z")

</div>

Hi,

I have a dataframe which is constructed using a list and other dataframe that i read from excel file.  
What I want to do is, I just have to apply the background color to first row of a dataframe which I would export in to an excel.  
The below code doing the job correclty as expected.(There is issue with the data)

The issue is the style which I have applied to the dataframe was not reflected in the excel sheet. I am using Jupyter Notebook.  
Please suggest a way to get the styles in excel.

`import pandas as pd

sheet1 = r’D:\dinesh\input.xlsx’  
sheet2 = “D:\dinesh\lookup.xlsx”  
sheet3 = “D:\dinesh\Output.xlsx”  
sheetname = ‘Dashboard Índice (INPUT)’

print(‘Started extracting the Crime Type!’)  
df1 = pd.read\_excel(sheet1,sheet\_name = ‘Dashboard Índice (INPUT)’,skiprows=10, usecols = ‘B,C,D,F,H,J,L,N,P’, encoding = ‘unicode\_escape’)

crime\_type = list(df1.iloc[:0])[3:]  
print(f’crime\_types : {crime\_type}’)  
df1 = (df1.drop(columns=crime\_type,axis=1))  
cols = list(df1.iloc[0])

print(f’Columns : {cols}’)  
df1.columns = cols  
df1 = (df1[1:]).dropna()

final\_data =   
for index, row in df1.iterrows():  
sheetname = (f’{row[cols[1]]:0\>2d}. {row[cols[0]]}’)  
cnty\_cd = [row[cols[0]], row[cols[1]], row[cols[2]]]  
wb = pd.ExcelFile(sheet2)

```
workbook = ''.join([workbook for workbook in wb.sheet_names if workbook.upper() == sheetname])         
if workbook:
	df2 = pd.read_excel(sheet2, sheet_name = workbook, skiprows=7, usecols ='C,D,H:T', encoding = 'unicode_escape')

	df2_cols = list(df2.columns)  
	final_cols = cols + df2_cols
	df2 = df2.iloc[2:]        
	df2 = df2.dropna(subset=[df2_cols[1]])

	for index2, row2 in df2.iterrows():             
		if row2[df2_cols[1]].upper() in crime_type:
			s1 = pd.Series(cnty_cd)                
			df_rows = (pd.concat([s1, row2], axis=0)).to_frame().transpose()             
			final_data.append(df_rows)
			break                                                                                 
else:
	print(f'{sheetname} does not exists!')

```

df3 = pd.concat(final\_data)  
df3.columns = final\_cols  
df\_cols = (pd.Series(final\_cols, index=final\_cols)).to\_frame().transpose()  
df\_final = (pd.concat([df\_cols,df3], axis=0, ignore\_index=True, sort=False))  
df\_final.style.apply(lambda x: [‘background: blue’ if x.name==0 else ‘’ for i in x], axis=1)  
df\_final.to\_excel(sheet3, sheet\_name=‘Crime Details’,index=False,header = None)

print(f’Sucessfully created the Output file to {sheet3}!’) `

---

<div class="post-metadata">

**Author:** ![tburnett](https://avatars.discourse-cdn.com/v4/letter/t/ac8455/32.png) [@tburnett](https://discuss.python.org/u/tburnett)\
**Post date:** [May 14, 2020, 5:22pm UTC](https://discuss.python.org/t/pandas-dataframe-styles-are-not-working-with-jupyter-notebook/3131/2 "2020-05-14T17:22:56Z")

</div>

Probably related to this, I find that the “classes” option of DataFrame.to\_html, as discussed [here](https://blog.hedaro.com/styling-dataframe-tables.html) seems to have no effect; and indeed the examples there show this. It is confusing that Jupyter output in the notebook, using _repr\_html_, shows formatting that I can’t reproduce with to\_html.
