In this section we will learn how to format integer column of Dataframe in Python pandas with an example. We will learn
- Round off a column values of dataframe to two decimal places
- Format the column value of dataframe with commas
- Format the column value of dataframe with dollar
- Format the column value of dataframe with scientific notation
Let’s see each with an example. First lest create a dataframe.
Create Dataframe:
# create dataframe import pandas as pd d = {'Quarters' : ['Quarter1','Quarter2','Quarter3','Quarter4'], 'Revenue':[23400344.567,54363744.678,56789117.456,4132454.987]} df=pd.DataFrame(d) print df
So the resultant dataframe will be
Round off the column values to two decimal places in python pandas:
# round to two decimal places in python pandas pd.options.display.float_format = '{:.2f}'.format print df
Format with commas and round off to two decimal places in python pandas:
# Format with commas and round off to two decimal places in pandas pd.options.display.float_format = '{:,.2f}'.format print df
Format with commas and Dollar sign with two decimal places in python pandas:
# Format with dollars, commas and round off to two decimal places in pandas pd.options.display.float_format = '${:,.2f}'.format print df
Format with Scientific notation in python pandas:
# Format with Scientific notation pd.options.display.float_format = '{:.2E}'.format print df