Coding Ref

How to round in Pandas

How to round in Pandas

To round a column in Pandas, you can use the round method. This method is applied to a Series object and returns a new Series object containing the rounded values for each element in the original Series.

For example, consider the following DataFrame:

main.py
import pandas as pd

df = pd.DataFrame({
    'A': [1.1, 2.2, 3.3, 4.4, 5.5],
    'B': [10.12, 20.23, 30.34, 40.45, 50.56],
    'C': [100.123, 200.234, 300.345, 400.456, 500.567]
})

This DataFrame has three columns A, B, and C, with five rows of data.

To round a specific column, you could do the following:

main.py
import pandas as pd

df = pd.DataFrame({
    'A': [1.1, 2.2, 3.3, 4.4, 5.5],
    'B': [10.12, 20.23, 30.34, 40.45, 50.56],
    'C': [100.123, 200.234, 300.345, 400.456, 500.567]
})

# Round column B to the nearest integer
rounded = df['B'].round()

# Print the resulting Series
print(rounded)
output
0    10.0
1    20.0
2    30.0
3    40.0
4    51.0
Name: B, dtype: float64

In the code above, the round method is applied to the B column of the DataFrame, which rounds each element in that column to the nearest integer.

The result is a new Series object containing the rounded values for the B column.

In this case, the resulting Series has five elements with the rounded values 10, 20, 30, 40, and 51.

Specify the number of decimal places to round to

You can also specify the number of decimal places to round to when using the round method.

For example, if you wanted to round column B to two decimal places, you could do the following:

main.py
import pandas as pd

df = pd.DataFrame({
    'A': [1.1, 2.2, 3.3, 4.4, 5.5],
    'B': [10.12, 20.23, 30.34, 40.45, 50.56],
    'C': [100.123, 200.234, 300.345, 400.456, 500.567]
})

# Round column B to two decimal places
rounded = df['B'].round(2)

# Print the resulting Series
print(rounded)
output
0    10.12
1    20.23
2    30.34
3    40.45
4    50.56
Name: B, dtype: float64

In the code above, the round method is applied to the B column of the DataFrame, and the number 2 is passed as an argument.

This tells the round method to round each element in the B column to two decimal places.

The result is a new Series object containing the rounded values for the B column.

Round multiple columns

You can also specify multiple columns when using the round method.

For example, if you wanted to round both columns B and C, you could do the following:

main.py
import pandas as pd

df = pd.DataFrame({
    'A': [1.1, 2.2, 3.3, 4.4, 5.5],
    'B': [10.12, 20.23, 30.34, 40.45, 50.56],
    'C': [100.123, 200.234, 300.345, 400.456, 500.567]
})

# Round column B and C to two decimal places
df[['B', 'C']] = df[['B', 'C']].round(2)

# Print the resulting DataFrame
print(df)
output
     A      B       C
0  1.1  10.12  100.12
1  2.2  20.23  200.23
2  3.3  30.34  300.35
3  4.4  40.45  400.46
4  5.5  50.56  500.57

This will round columns B and C to two decimal places, but leave column A unchanged.

The resulting dataframe will have the same structure as the original dataframe, but the values in columns B and C will be rounded to two decimal places.

You'll also like

Related tutorials curated for you

    How to find the mode in a Pandas DataFrame

    How to write a Pandas DataFrame to SQL

    How to use str.split() in Pandas

    How to drop an index column in Pandas

    What does Head() do in Pandas?

    How to use ewm() in Pandas

    What is categorical data in Pandas?

    How to calculate covariance in Pandas

    What is isna() in Pandas?

    How to change the order of columns in Pandas

    How to convert a Pandas Index to a List

    How to give multiple conditions in loc() in Pandas