Skip to content
FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS
Close
Beyond Knowledge Innovation

Beyond Knowledge Innovation

Where Data Unveils Possibilities

  • Home
  • AI & ML Insights
  • Machine Learning
    • Supervised Learning
      • Introduction
      • Regression
      • Classification
    • Unsupervised Learning
      • Introduction
      • Clustering
      • Association
      • Dimensionality Reduction
    • Reinforcement Learning
    • Generative AI
  • Knowledge Base
    • Introduction To Python
    • Introduction To Data
    • Introduction to EDA
  • References
HomeKnowledge BasePythonHow-to: formatting options for floating-point numbers in Pandas
Python

How-to: formatting options for floating-point numbers in Pandas

February 2, 2024February 2, 2024CEO 333 views
In Pandas, pd.set_option('display.float_format', ...) is used to set the formatting options for floating-point numbers when they are displayed in the console or output. It allows you to customize how floating-point numbers are presented, including the number of decimal places, scientific notation, and other formatting details.

import pandas as pd

# Sample DataFrame
data = {'A': [1234567.890123456789, 9876543.210987654321]}
df = pd.DataFrame(data)

# Set display float format to show 4 decimal places
pd.set_option('display.float_format','{:,.2f}'.format)

# Display the DataFrame
print(df)

             A
0 1,234,567.89
1 9,876,543.21

In this example, the pd.set_option('display.float_format', '{:,.2f}'.format) line sets the floating-point number format to display up to two decimal places with comma as a thousand separator. The formatting options are specified using a format string, where '{:,.2f}' indicates that each floating-point number should be displayed with up to two decimal places.

You can customize the format string to meet your specific formatting requirements. Here are some examples:

  • '{:.2f}': Display up to two decimal places.
  • '{:,.2f}': Display up to two decimal places with a comma as a thousands separator.

Note that this setting affects the display of floating-point numbers in Pandas DataFrames when you print or view them in a Jupyter Notebook or console. It doesn’t change the actual values in the DataFrame; it only affects their presentation.

Using a Lambda function:

pd.set_option('display.float_format', lambda x: '%.2f' % x)
data, formatting, lambda, pandas, python

Post navigation

Previous Post
Previous post: Univariate Analysis in EDA
Next Post
Next post: How-to: clean a dataset

You Might Also Like

No image
Delete a folder in Google Colab
June 20, 2024 Comments Off on Delete a folder in Google Colab
No image
Quantile-based discretization of continuous variables
April 29, 2024 Comments Off on Quantile-based discretization of continuous variables
No image
CDF plot of Numerical columns
March 12, 2024 Comments Off on CDF plot of Numerical columns
No image
Get a random sample from your dataset
March 7, 2024 Comments Off on Get a random sample from your dataset
No image
Python warnings module
March 3, 2024 Comments Off on Python warnings module
  • Recent
  • Popular
  • Random
  • No image
    7 months ago Low-Rank Factorization
  • No image
    7 months ago Perturbation Test for a Regression Model
  • No image
    7 months ago Calibration Curve for Classification Models
  • No image
    March 15, 20240Single linkage hierarchical clustering
  • No image
    April 17, 20240XGBoost (eXtreme Gradient Boosting)
  • No image
    April 17, 20240Gradient Boosting
  • No image
    January 16, 2024Handling missing data in a dataset
  • No image
    March 11, 2024What is Silhouette Coefficient
  • No image
    January 19, 2024NumPy View array vs. Copy array
  • Implementation (55)
    • EDA (4)
    • Neural Networks (10)
    • Supervised Learning (26)
      • Classification (17)
      • Linear Regression (8)
    • Unsupervised Learning (11)
      • Clustering (8)
      • Dimensionality Reduction (3)
  • Knowledge Base (44)
    • Python (27)
    • Statistics (6)
May 2025
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Oct    

We are on

FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS

Subscribe

© 2025 Beyond Knowledge Innovation
FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS