Search code examples
htmlpandasdataframeipythongoogle-colaboratory

Left align the first column and center align the other columns in a Pandas table


I am trying to format a Pandas table. I want to left align the first column and center align the other two columns in a Pandas table (except for the table header).

Code:

import pandas as pd
from IPython.display import HTML
df = pd.DataFrame({'Unit': ['Bit', 'Nibble','Byte/Octet', 'Kilobyte', 'Megabyte', 'Gigabyte', 'Terabyte'], 'Abbreviation': ['b', '-', 'B', 'KB', 'MB', 'GB', 'TB'], 'Storage': ['Binary digit, single 0 or 1', '4 bits', '8 bits', '1024 bytes', '1024 KB', '1024 MB', '1024 GB']})
df.style.set_properties(**{'text-align': 'center'})

I'm using Google Colab to render the table.

Results:

Results in Google Colab

This table is almost what I want. I'm trying to print the same table except that I want to hide the index numbers and left align the first column, so that I have a table with 7 rows + 1 header and 3 columns that looks like this.

enter image description here

I tried using df.style.hide_index() to hide the index numbers and it worked, but I couldn't figure out how to combine df.style.hide_index() with df.style.set_properties(**{'text-align': 'center'}) The first screenshot shows all three columns are center aligned and I want to left align the first column and center align the 2nd and 3rd columns, except for the header which should have all three columns center aligned.


Solution

  • The table can be pretty formatted in Pandas by assembling the two missing formatting conditions into a single df. I made the following two changes to the original code.

    1. Hide index numbers with hide_index()

      df[["Unit", "Abbreviation", "Storage"]].style.hide_index()  
      
    2. To apply to a subset of columns, you can use the subset parameter. Left align the first column by default and center align the other 2 columns using the subset parameter.

      set_properties(subset=["Abbreviation", "Storage"], **{'text-align': 'center'})
      

    Code:

    import pandas as pd
    from IPython.display import HTML
    df = pd.DataFrame({'Unit': ['Bit', 'Nibble','Byte/Octet', 'Kilobyte', 'Megabyte', 'Gigabyte', 'Terabyte'], 'Abbreviation': ['b', '-', 'B', 'KB', 'MB', 'GB', 'TB'], 'Storage': ['Binary digit, single 0 or 1', '4 bits', '8 bits', '1024 bytes', '1024 KB', '1024 MB', '1024 GB']})
    df[["Unit", "Abbreviation", "Storage"]].style.hide_index().set_properties(subset=["Abbreviation", "Storage"], **{'text-align': 'center'})
    

    Results:

    Pandas table

    Usage:

    Let's say that you have an Excel spreadsheet and you want to print a custom formatted table that looks better than Excel's built-in table templates. All you need to do is open the spreadsheet in Excel and export it as a csv file. Then run the following Python code to convert the csv file to a Pandas df.

    import pandas as pd
    filepath = '/path/to/FILE.csv' # replace with an existing path to FILE.csv
    df = pd.read_csv(filepath)
    df
    

    Use this df to make a custom formatted table.