1st question: I am trying to modify the style of a dataframes and then save it on an excel file. The main body is modified as wanted but the headers for some reason are not changed when creating the excel output file.
import pandas as pd
def style_columnns(df, highlight=False):
styles = [dict(selector='th',
props=[('text-align', 'center'),
('font-size', '12px'),
('font-family', 'Arial')])]
return df.style.set_table_styles(styles).set_properties(**{'text-align':'center','font-size':'12pt','font-family':'arial'})
pd.ExcelWriter('excel_title') as writer:
style_columnns(my_df).to_excel(writer, sheet_name='sheet name', index = False)
writer.save()
The format of the headers in the excel file are Calibri with font-size 11, bold and text-align to the center.
I want them to be Arial with font-size 12, bold and text-align to the center.
Any ideas what am i missing?
2nd question: The header names of my dataframes are 'A', 'B', 'C' and 'D'. The header 'A' is not always present. If the dataframe contains the header 'A', i want the header and the values to be left aligned. How can i change that? The code for the column values is as follows but i cannot left align the header.
my_df.set_properties(subset=["A"],**{'text-align': 'left', 'font-style':'italic','font-size': '12pt', 'font-family':'arial'})
None of the previous questions on stackoverflow have solved my issue.
Thank you in advance!