Formatting float column of Dataframe in Pandas (original) (raw)
Last Updated : 10 Jan, 2024
While presenting the data, showing the data in the required format is also a crucial part. Sometimes, the value is so big that we want to show only the desired part of this or we can say in some desired format. Let’s see different methods of formatting integer columns and **the data frame **it in Pandas.
Formatting float column of Dataframe in Pandas
Here, are various ways to Number formatting in Pandas in Python. So we are explaining some generally used **ways and methods for Number formatting in Pandas in the following.
Round off the Column Values to Two Decimal Places
In this example, the below code uses the pandas’ library to create a Pandas DataFrame named ‘data frame’ from a dictionary containing monthly expenses. It then prints the original data frame. After setting the pandas option to display float values with two decimal places.
Python3
import
pandas as pd
data
=
{
'Month'
: [
'January'
,
'February'
,
'March'
,
'April'
],
`` 'Expense'
: [
21525220.653
,
31125840.875
,
23135428.768
,
56245263.942
]}
dataframe
=
pd.DataFrame(data, columns
=
[
'Month'
,
'Expense'
])
print
(
"Given Dataframe :\n"
, dataframe)
pd.options.display.float_format
=
'{:.2f}'
.
format
print
(
'\nResult :\n'
, dataframe)
**Output:
**Given Dataframe :
Month Expense
0 January 2.152522e+07
1 February 3.112584e+07
2 March 2.313543e+07
3 April 5.624526e+07
**Result :
Month Expense
0 January 21525220.65
1 February 31125840.88
2 March 23135428.77
3 April 56245263.94
**Pandas DataFrame Formatting with Commas and Decimal Precision
In this example below code uses pandas to create a DataFrame, ‘products_dataframe,’ with product names and their respective prices. It prints the initial DataFrame and then formats the ‘Price’ column with commas and rounds the values to two decimal places.
Python3
import
pandas as pd
data
=
{
'Product'
: [
'Laptop'
,
'Phone'
,
'Tablet'
,
'Desktop'
],
`` 'Price'
: [
1200.50
,
799.99
,
349.75
,
1500.25
]}
products_dataframe
=
pd.DataFrame(data, columns
=
[
'Product'
,
'Price'
])
print
(
"Given Dataframe :\n"
, products_dataframe)
pd.options.display.float_format
=
'{:,.2f}'
.
format
formatted_products
=
products_dataframe.copy()
formatted_products[
'Price'
]
=
formatted_products[
'Price'
].
apply
(
lambda
x:
'{:,.2f}'
.
format
(x))
print
(
'\nResult :\n'
, formatted_products)
**Output:
**Given Dataframe :
Product Price
0 Laptop 1,200.50
1 Phone 799.99
2 Tablet 349.75
3 Desktop 1,500.25
**Result :
Product Price
0 Laptop 1,200.50
1 Phone 799.99
2 Tablet 349.75
3 Desktop 1,500.25
Formatting and Scaling Population Data in Pandas DataFrame
In this example code utilizes the pandas library to create a DataFrame named ‘city_dataframe’ with city names and their respective populations. Initially displaying the DataFrame, it later formats the population column with commas and scales the values to millions.
Python3
import
pandas as pd
data
=
{
'City'
: [
'New York'
,
'Los Angeles'
,
'Chicago'
,
'Houston'
],
`` 'Population'
: [
8336817
,
3980400
,
2716000
,
2328000
]}
city_dataframe
=
pd.DataFrame(data, columns
=
[
'City'
,
'Population'
])
print
(
"Given DataFrame:\n"
, city_dataframe)
pd.options.display.float_format
=
'{:,.2f}'
.
format
city_dataframe[
'Population'
]
=
city_dataframe[
'Population'
]
/
1000000
print
(
'\nResult:\n'
, city_dataframe)
**Output:
**Given DataFrame:
City Population
0 New York 8336817
1 Los Angeles 3980400
2 Chicago 2716000
3 Houston 2328000
**Result:
City Population
0 New York 8.34
1 Los Angeles 3.98
2 Chicago 2.72
3 Houston 2.33