Format integer column of Dataframe in Python pandas

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

Format integer column of Dataframe in Python pandas 1

 

 

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 integer column of Dataframe in Python pandas 2

 

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 integer column of Dataframe in Python pandas 3

 

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 integer column of Dataframe in Python pandas 4

 

Format with Scientific notation in python pandas:

# Format with Scientific notation

pd.options.display.float_format = '{:.2E}'.format
print df

Format integer column of Dataframe in Python pandas 5

previous Format integer column of Dataframe in Python pandas                                                                                                            next Format integer column of Dataframe in Python pandas

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.