I have a CSV file I add the code to process data on it and know how to save the final dataframe to the same CSV by using to_csv method. The problem is I want to add the background color to some of the columns, how can I do it?

I strongly suggest reading the guide in the docs
To see an example where column names are styled see this post by Scott Boston
style with applydf = pd.DataFrame([[0, 1], [2, 3]], ['A', 'B'], ['X', 'Y'])
def f(dat, c='red'):
return [f'background-color: {c}' for i in dat]
df.style.apply(f, axis=0, subset=['X'])
columns_with_color_dictionary = {'X': 'green', 'Y': 'cyan'}
style = df.style
for column, color in columns_with_color_dictionary.items():
style = style.apply(f, axis=0, subset=column, c=color)
style
df.rename(columns=lambda x: f"{x}_{columns_with_color_dictionary.get(x)}") \
.to_csv('colorful_df.csv')
df_color = pd.read_csv('colorful_df.csv', index_col=0)
cmap = dict([c.split('_', 1) for c in df_color])
df_color.columns = df_color.columns.str.split('_', 1).str[0]
style = df_color.style
for column, color in cmap.items():
style = style.apply(f, axis=0, subset=column, c=color)
style
HTMLfrom IPython.display import HTML
columns_with_color_dictionary = {'X': 'yellow', 'Y': 'orange'}
style = df.style
for column, color in columns_with_color_dictionary.items():
style = style.apply(f, axis=0, subset=column, c=color)
with open('df.html', 'w') as fh:
fh.write(style.render())
HTML(open('df.html').read())
pd.ExcelWriter, saving the results in .xlsx will allow you to color the column background.