📅  最后修改于: 2023-12-03 15:33:23.821000             🧑  作者: Mango
When working with data in pandas, it is often necessary to round floats to a certain number of decimal places. This can be done using the round()
method that is available in pandas. In this article, we will discuss how to use the round()
method to round floats in pandas.
The round()
method can be used on a pandas Series or DataFrame to round the values in the data to a specified number of decimal places. The basic usage of the round()
method is shown below:
import pandas as pd
# create a pandas Series
s = pd.Series([1.1111, 2.2222, 3.3333, 4.4444])
# round the values to 2 decimal places
s_rounded = s.round(2)
print(s_rounded)
Output:
0 1.11
1 2.22
2 3.33
3 4.44
dtype: float64
In the above example, we created a pandas Series s
with four float values. We then used the round()
method to round the values to 2 decimal places and stored the result in a new Series s_rounded
. The output shows the rounded values in the new Series.
In addition to rounding to a certain number of decimal places, the round()
method can also be used to round to the nearest integer. To do this, we simply pass 0 as the precision parameter. The code below demonstrates rounding a Series to the nearest integer:
# create a pandas Series
s = pd.Series([1.1, 2.5, 3.7, 4.9])
# round the values to the nearest integer
s_rounded = s.round(0)
print(s_rounded)
Output:
0 1.0
1 2.0
2 4.0
3 5.0
dtype: float64
We can also use the round()
method to round the values of a specific column in a pandas DataFrame. To do this, we need to select the column we want to round and apply the round()
method to it. The following code demonstrates rounding the col1
column of a DataFrame to 2 decimal places:
# create a pandas DataFrame
df = pd.DataFrame({'col1': [1.1111, 2.2222, 3.3333, 4.4444],
'col2': [1.1, 2.5, 3.7, 4.9]})
# round the values in the 'col1' column to 2 decimal places
df['col1'] = df['col1'].round(2)
print(df)
Output:
col1 col2
0 1.11 1.1
1 2.22 2.5
2 3.33 3.7
3 4.44 4.9
In the above example, we created a DataFrame df
with two columns col1
and col2
. We then rounded the values in the col1
column to 2 decimal places and stored the result back into the same column of the DataFrame.
It is important to note that the round()
method uses rounding rules to determine how to round the values in the data. The default rounding rule is to round to the nearest even number when the value is exactly midway between two integers. For example, if we round 2.5 to the nearest integer, the result will be 2 because 2 is even. However, if we round 3.5 to the nearest integer, the result will be 4 because 4 is even. If you want to change this behavior, you can pass a decimal.ROUND_
constant to the round()
method. The available rounding modes include:
ROUND_UP
: round away from zeroROUND_DOWN
: round towards zeroROUND_CEILING
: round towards infinityROUND_FLOOR
: round towards negative infinityROUND_HALF_UP
: round to nearest with ties going away from zeroROUND_HALF_DOWN
: round to nearest with ties going towards zeroROUND_HALF_EVEN
: round to nearest with ties going to nearest even integerROUND_05UP
: round up if the last digit after rounding is 0 or 5The decimal.ROUND_HALF_EVEN
is the default rounding mode used by pandas, but you can change it by passing a different constant to the round()
method.
The round()
method is a simple and useful method for rounding floats in pandas. By adjusting the precision parameter, you can control the number of decimal places in the rounded values. You can also use the round()
method to round to the nearest integer and round specific columns in a DataFrame. Always keep in mind the rounding rules and adjust them if necessary using the constants available in the decimal
module.