How to color text output from Pandas Dataframe in terminal (Linux)?

Question:

I want to be able to print data from data_dict with different colors based on the positive or negative value with Pandas DataFrame text in a linux terminal.

data_dict = {"Key1":[1,-2,3], "Key2":[1,2,-3]}

df = pd.DataFrame(data_dict)
print(df)

Is there a way to use colorama.Fore or something similar to somehow update the color of different cells as they are printed in the terminal?

Asked By: Josh Crouse

||

Answers:

Yes, there is a way, here is an example solution:

import pandas as pd
from termcolor import colored

data_dict = {"Key1":[1,-2,3], "Key2":[1,2,-3]}

df = colored(pd.DataFrame(data_dict), 'red')
print(df)

https://pypi.org/project/termcolor/

Edit:

also if you dont want to use imports

you can define a color and iterate the dic for the key values:

red = "33[1;31m"
blue = "33[1;34m"
data_dict = {"Key1":[1,-2,3], "Key2":[1,2,-3]}
for key in data_dict.keys () :
    print (red, key, blue, data_dict[key])

then you just put your condition on the key for the color

Answered By: Cayman

To get a colored pandas.DataFrame printed to my macos terminal, I used the following code…

import termcolor
import pandas
# assume `df['foo']` is a `pandas.DataFrame` column with
#     boolean values...
#
df = pandas.DataFrame({'foo': [False]}) # this is a fake df with
                                        # no real data.  Ensure 
                                        # you have a real
                                        # DataFrame stored in 
                                        # df...

# NOTE: there's probably a more idiomatic pandas incantation 
# for what I'm doing here, but this is intended to 
# demonstrate the concept of colorizing `colorized_df['foo']`
colorized_df = None
colored_str_value = ""
colorised_row = []
for original_value in df['foo'].values:
    # casting `original_value` bools as strings to color them red...
    colored_str_value = termcolor.colored(str(original_value), 'red')
    colorized_row.append(colored_str_value)
colorized_df = pandas.DataFrame({'foo': colorized_row})
# Do something here with colorized_df...

Now when I print colorized_df in the terminal, it renders colorized_df['foo'] with red text…

As such, colorized_df cell values must be cast as a strings to color the DataFrame contents. For instance, df['foo'] has boolean data; those booleans have to be cast as strings in colorized_df[ foo'] (otherwise the values can’t be colored).

For most use-cases, re-casting your original DataFrame as string is a non-starter… one possible solution (used in my answer) -> keep two copies of the DataFrame… (A) one copy to render as the original DataFrame type in df['foo']… (B) the other copy to manipulate freely and cast to colorized-strings…

However, you must keep those two DataFrames synchronized if you modify df['foo'].

Answered By: Mike Pennington