The round()
function in pandas is used to round up a DataFrame to a specified number of decimal places.
The round()
function takes the syntax shown below:
DataFrame.round(decimals=0, *args, **kwargs)
The round()
function takes the following optional parameter values:
decimals
: This takes an int
value specifying the number of decimal places to round the values of the DataFrame.*args
, **kwargs
: These are additional keywords that have no effect on the function but might be accepted for compatibility with the NumPy library.The round()
function returns a DataFrame holding the rounded values of the columns of the DataFrame.
# A code to illustrate the round() functio in Pandas# importing the pandas libraryimport pandas as pd# creating a dataframedf = pd.DataFrame([(.521, .132), (.019, .675), (.666, .033), (.555, .4489)],columns=['col_1', 'col_2'])print("Before dataframe")print(df) # printing the dataframeprint("After dataframe")# rounding up the values of the dataframe to 2 decimal placesprint(df.round(2))
pandas
library.df
.df
.2
decimal places.