Python | Pandas dataframe.round() - GeeksforGeeks

30 Aug, 2021
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas dataframe.round() function is used to round a DataFrame to a variable number of decimal places. This function provides the flexibility to round different columns by different places.
Syntax:DataFrame.round(decimals=0, *args, **kwargs)Parameters :decimals : Number of decimal places to round each column to. If an int is given, round each column to the same number of places. Otherwise dict and Series round to variable numbers of places. Column names should be in the keys if decimals is a dict-like, or in the index if decimals is a Series. Any columns not included in decimals will be left as is. Elements of decimals which are not columns of the input will be ignored.
Returns : DataFrame object
Example #1: Use round() function to round off all columns in the dataframe to 3 decimal places
Note : We need to populate our dataframe with decimal values. Let’s use numpy random function to achieve the task.
# importing pandas as pdimport pandas as pd  # importing numpy as npimport numpy as np  # setting the seed to re-create the dataframenp.random.seed(25)  # Creating a 5 * 4 dataframe df = pd.DataFrame(np.random.random([5, 4]), columns =["A", "B", "C", "D"])  # Print the dataframedf
Lets use the dataframe.round() function to round off all the decimal values in the dataframe to 3 decimal places.
df.round(3)
Output : Example #2: Use round() function to round off all the columns in dataframe to different places.
# importing pandas as pdimport pandas as pd  # importing numpy as npimport numpy as np  # setting the seed to re-create the dataframenp.random.seed(25)  # Creating a 5 * 4 dataframe df = pd.DataFrame(np.random.random([5, 4]), columns =["A", "B", "C", "D"])  # Print the dataframedf
Lets perform round off each column to different places
# round off the columns in this manner# "A" to 1 decimal place# "B" to 2 decimal place# "C" to 3 decimal place# "D" to 4 decimal place  df.round({"A":1, "B":2, "C":3, "D":4})
Output :
YouTubeGeeksforGeeks507K subscribersPython | Pandas dataframe.round() | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:18•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=rOnKa_tzV04" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Python pandas-dataFrame
Python pandas-dataFrame-methods
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists
Convert integer to string in Python
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?