Tuesday, November 15, 2022

AI/ML How to print Dataframes in style

The approaches are if using IPython, then use the display 

from IPython.display import display

display(df)


If is also possible to apply the style using the below 


df.style


def color_negative_red(val):

    """

    Takes a scalar and returns a string with

    the css property `'color: red'` for negative

    strings, black otherwise.

    """

    color = 'blue' if val > 90 else 'black'

    return 'color: % s' % color


df.style.applymap(color_negative_red)


If using print, then tabulate is a good option 


from tabulate import tabulate


print (tabulate(df, headers = 'keys', tablefmt = 'psql'))

There are many table formats available such other than psql 


with psql, it looks like this below 


references:

https://www.geeksforgeeks.org/display-the-pandas-dataframe-in-table-style/ 


No comments:

Post a Comment