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.
-
Hide index numbers with
hide_index()
df[["Unit", "Abbreviation", "Storage"]].style.hide_index()
-
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 thesubset
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:
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.
solved Left align the first column and center align the other columns in a Pandas table