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.
*argsAdditional keywords have no effect but might be accepted for compatibility with numpy.
**kwargsAdditional keywords have no effect but might be accepted for compatibility with numpy.
ReturnsDataFrameA DataFrame with the affected columns rounded to the specified number of decimal places.
See also
numpy.around
Round a numpy array to the given number of decimals.
Series.round
Round a Series to the given number of decimals.
Examples
>>> df = pd.DataFrame[[[.21, .32], [.01, .67], [.66, .03], [.21, .18]], ... columns=['dogs', 'cats']] >>> df dogs cats 0 0.21 0.32 1 0.01 0.67 2 0.66 0.03 3 0.21 0.18
By providing an integer each column is rounded to the same number of decimal places
>>> df.round[1] dogs cats 0 0.2 0.3 1 0.0 0.7 2 0.7 0.0 3 0.2 0.2
With a dict, the number of places for specific columns can be specified with the column names as key and the number of decimal places as value
>>> df.round[{'dogs': 1, 'cats': 0}] dogs cats 0 0.2 0.0 1 0.0 1.0 2 0.7 0.0 3 0.2 0.0
Using a Series, the number of places for specific columns can be specified with the column names as index and the number of decimal places as value
Determines which duplicates [if any] to keep. - first
: Drop duplicates except for the first occurrence. - last
: Drop duplicates except for the last occurrence. - False : Drop all duplicates.
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.
Pandasdataframe.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 pd
import
pandas as pd
# importing numpy as np
import
numpy as np
round[]
0
round[]
1round[]
2round[]
3
round[]
5
round[]
6round[]
7 round[]
8round[]
9# importing pandas as pd
0# importing pandas as pd
1# importing pandas as pd
2round[]
7# importing pandas as pd
4# importing pandas as pd
5# importing pandas as pd
0# importing pandas as pd
7# importing pandas as pd
0# importing pandas as pd
9# importing pandas as pd
0import
1import
2
import
4
round[]
6
Lets use the import
6 function to round off all the decimal values in the dataframe to 3 decimal places.
import
7import
8import
9pandas as pd
0round[]
3
Output :
Example #2: Use
round[]
function to round off all the columns in dataframe to different places.# importing pandas as pd
import
pandas as pd
# importing numpy as np
import
numpy as np
round[]
0
round[]
1round[]
2round[]
3
round[]
5
round[]
6round[]
7 round[]
8round[]
9# importing pandas as pd
0# importing pandas as pd
1# importing pandas as pd
2round[]
7# importing pandas as pd
4# importing pandas as pd
5# importing pandas as pd
0# importing pandas as pd
7# importing pandas as pd
0# importing pandas as pd
9# importing pandas as pd
0import
1import
2
import
4
round[]
6
Lets perform round off each column to different places
import
7
import
8
import
9
numpy as np
0
numpy as np
1
import
7import
8numpy as np
5# importing pandas as pd
5numpy as np
7numpy as np
8# importing pandas as pd
0# importing pandas as pd
7numpy as np
7
2# importing pandas as pd
0# importing pandas as pd
9numpy as np
7pandas as pd
0# importing pandas as pd
0import
1numpy as np
7# importing pandas as pd
1round[]
01