pythonpandaslinuxterminalcolorama

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


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?


Solution

  • 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 = ""
    colorized_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'].