Try:
df = df.style.apply(lambda x: np.repeat('color: red' if x['error'] else '', x.shape[0]), axis=1)
When output to Excel it looks like...

How do I make the categories have a coloured background when using the rich modules?
python - changing value of every cell in dataframe to color the table - Stack Overflow
Is it possible to style a table with rows having alternating background colours?
python - Set different background color using Rich - Stack Overflow
So I made this table and basically want the cell of each column's title to have a color program. How can I do this using the rich module (I'm using Replit rn)
from rich.console import Console #Allows for styling of text from rich.table import Table #For clear formatting and titles from colorama import Fore, Back, Style #To add colors
Info for each flight includng time, destination, airport code, flight #, terminal, carousel, and status
Title1 = Table(title="Arrivals for FEBRUARY 9, 2024\n") r2 = [[" 10:00","Regina","YQR","AC301","1","5","On Time"],
[" 12:00","Thunder Bay","YQT","AC452","2","7","Arrived"], [" 14:00","Winnipeg","YWG","AC326","3", "7","On Time"], [" 16:00","Ottawa","YOW","AC244","3","3","On Time"], [" 10:00","Yellowknife","YZF","AC521","1","8", "Arrived"], [" 12:00","PEI","YYG","AC403","2","4","On Time"], [" 14:00","Halifax","YHZ","AC322","1","2","Cancelled"], [" 16:00","Edmonton","YEG","AC208","4","1","On Time"], [" 10:00","Hamilton","YHM","AC333","1","6","Cancelled"], [" 12:00","Montreal","YUL","AC997","1","8","Delayed"], [" 14:00","Vancouver","YVR","AC654","3","2","Cancelled"], [" 16:00","Calgary","YYC","AC878","2","1","Arrived"]]
Title of categories (Bolded in white)
cat = ["ARRIVAL TIME","ARRIVING FROM","DESTINATION CODE","FLIGHT NUMBER"," TERMINAL", "CAROUSEL NUMBER"," STATUS",]
Each heading gets its own column
for column in cat: Title1.add_column(column)
Title2 = Table(title="\nDepartures for FEBRUARY 9, 2024\n")
r1 = [[" 10:30","Regina","YQR","AC321","1","3","On Time",], [" 13:00","Thunder Bay","YQT","AC665","2","1","Delayed"], [" 15:00","Winnipeg","YWG","AC332","3","5","On Time"], [" 17:00","Ottawa","YOW","AC244","3","5","On Time"], [" 11:00","Yellowknife","YZF","AC521","1","6","Arrived"], [" 15:00","PEI","YYG","AC403","2","4","On Time"], [" 17:00","Halifax","YHZ","AC451","1","5","Cancelled"], [" 17:00","Edmonton","YEG","AC909","4","1","On Time"], [" 18:00","Hamilton","YHM","AC313","1","7","Cancelled"], [" 19:00","Montreal","YUL","AC999","1","4","Delayed"], [" 21:00","Vancouver","YVR","AC623","3","5","Cancelled"], [" 23:00","Calgary","YYC","AC859","2","3","Delayed"]]
cat2 = ["DEPARTURE TIME","DESTINATION", "DESTINATION CODE","FLIGHT NUMBER"," TERMINAL","CAROUSEL NUMBER", " STATUS"]
Centers info to make it look nice
for i in r1: i[5] = i[5].center(15) i[4] = i[4].center(14) i[3] = i[3].center(14) i[2] = i[2].center(14)
for i in r2: i[5] = i[5].center(15) i[4] = i[4].center(14) i[3] = i[3].center(14) i[2] = i[2].center(14)
for column in cat2: Title2.add_column(column)
I begin coloring the text under the Status section according to the title using if, elif, and else statements
for row in r1: sy=row[6] if sy=="On Time": style='green' elif sy=="Cancelled": style='red' elif sy=="Arrived": style='blue' elif sy=="Delayed": style='yellow' else: style='white'
Adds styled rows
Title2.add_row(*row, style=style)
for row in r2: sy=row[6] if sy=="On Time": style='green' elif sy=="Cancelled": style='red' elif sy=="Arrived": style='blue' elif sy=="Delayed": style='yellow' else: style='white' Title1.add_row(*row, style=style)
Thanks for any help!
Try:
df = df.style.apply(lambda x: np.repeat('color: red' if x['error'] else '', x.shape[0]), axis=1)
When output to Excel it looks like...

I wanted to do the same as CIsForCookies and color cells of a dataframe in a cmd console window. I took the same approach by adding colorama color tags to the df cells, which also messed up resp. misaligned the printed dataframe. I also tried to insert rich color tags, which caused the same misalignment.
Then I found this discussion on the pandas GitHub page about styling console/terminal output So, manipulating the dataframe for getting colors doesnΒ΄t seem to work.
But the rich module was mentioned in the last post and I know you can create colorful tables with it. So I tried pulling the last rows of my df to a json object, iterate through the json data and add colors where I need them and build a rich table out of this altered json data - and it worked :-)
Some code...
Get the last 10 rows of df as an interable json data
json_data = loads(df.tail(10).to_json(orient='index'))As an example, iterate through the json data and add green or red color background if the close is above or below the 20 EMA:
for index in json_data: if json_data[index]['close'] >= json_data[index]['20EMA']: json_data[index]['20EMA'] = f"[black on green]{json_data[index]['20EMA']}[/]" else: json_data[index]['20EMA'] = f"[black on red]{json_data[index]['20EMA']}[/]"Create a rich table and add rows from the json data:
for index in json_data: table.add_row(index, str(local_time), str(f"{json_data[index]['close']:.2f}"), str(int(json_data[index]['volume'])), str(json_data[index]['20EMA']), )
And finally, there you have a table from a df with different cell colors.
