0

I'm trying to style my table but I'm having a problem because of how my table is made. I want to color individual column which i chose, for every year, but i don't want to involve years in code because i have many tables with different years so it needs to stay this format of table.

I need to apply different gradient colors for 4 columns. This is my code but it won't work. This is a Dropbox link of excel document.

import pandas as pd
import numpy as np

df = pd.read_excel('products2.xlsx', index_col=[0])

df.columns = df.columns.str.split('_', expand=True)

new_data = df.stack(0)
new_data1 = new_data.eval('status = profit - loss + other')
new_data2 = new_data1.eval('index = (profit / status) / (loss / status)')
# if i put here styling my data it will work untill output, which means in the end is not working
#style = new_data2.style.background_gradient(subset=['status'])

output = new_data2.unstack(1).swaplevel(0,1, axis=1).sort_index(axis=1)
style = output.style.background_gradient(subset=['profit', 'loss', 'status', 'index'])
# i need to profit to be green gradient, loss red gradient, status blue and index to be some color
style.to_excel('output_products.xlsx')

Any ideas?

project.py
  • 107
  • 1
  • 4
  • 19
  • I find [this answer](https://stackoverflow.com/questions/47391948/pandas-style-background-gradient-using-other-dataframe) to be very helpful – r-beginners Aug 06 '21 at 14:14
  • this is a bad question. you should provide a minimal reproducible example of your problem rather than providing a dropbox link to a full document. – Attack68 Aug 06 '21 at 18:30
  • This is a minimal reproducible example, I don't know how better to explain a problem. – project.py Aug 06 '21 at 19:02

0 Answers0