# 4 Methods to Round Values in Pandas DataFrame

Depending on the scenario, you may use either of the 4 methods below in order to round values in pandas DataFrame:

(1) Round to specific decimal places – Single DataFrame column

```df['DataFrame column'].round(decimals=number of decimal places needed)
```

(2) Round up – Single DataFrame column

```df['DataFrame column'].apply(np.ceil)
```

(3) Round down – Single DataFrame column

```df['DataFrame column'].apply(np.floor)
```

(4) Round to specific decimals places – Entire DataFrame

`df.round(decimals=number of decimal places needed)`

Let’s now see how to apply the 4 methods to round values in pandas DataFrame.

## Methods to Round Values in Pandas DataFrame

### Method 1: Round to specific decimal places – Single DataFrame column

Suppose that you have a dataset which contains the following values (with varying-length decimal places):

 Value 5.52132 6.572935 7.21 8.755 9.9989

You can then create a DataFrame to capture those values in Python:

```from pandas import DataFrame

Sample = {'Value': [5.52132,6.572935,7.21,8.755,9.9989]}
df = DataFrame(Sample, columns= ['Value'])

print(df)
```

The DataFrame would look like this in Python: Let’s say that your goal is to round the values into 3 decimals places.

Recall that the first method to round to specific decimals places (for a single DataFrame column) is:

`df['DataFrame Column'].round(decimals=number of decimal places needed)`

Therefore, for our example, in order to perform the rounding to 3 decimals places, you’ll need to add this syntax:

`df['Value'].round(decimals=3)`

So the full Python code would look like this:

```from pandas import DataFrame

Sample = {'Value': [5.52132,6.572935,7.21,8.755,9.9989]}
df = DataFrame(Sample, columns= ['Value'])

roundThree = df['Value'].round(decimals=3)
print(roundThree)
```

You’ll notice that the values are now rounded to 3 decimals places: Alternatively, you could also use numpy to round the values to 3 decimals places (for a single DataFrame column):

`np.round(df['DataFrame column'], decimals=number of decimal places needed)`

So this is how the Python code would look like for our example:

```from pandas import DataFrame
import numpy as np

Sample = {'Value': [5.52132,6.572935,7.21,8.755,9.9989]}
df = DataFrame(Sample, columns= ['Value'])

roundThree = np.round(df['Value'], decimals=3)
print(roundThree)
```

You’ll get the same results using numpy: ### Method 2: Round up – Single DataFrame column

What if you want to round up the values in your DataFrame?

To accomplish this goal, you can use the second method to round up values:

`df['DataFrame Column'].apply(np.ceil)`

In the context of our example, you’ll need to use this syntax:

`df['Value'].apply(np.ceil)`

Here is the complete Python code to round the values up using numpy:

```from pandas import DataFrame
import numpy as np

Sample = {'Value': [5.52132,6.572935,7.21,8.755,9.9989]}
df = DataFrame(Sample, columns= ['Value'])

roundUp = df['Value'].apply(np.ceil)
print (roundUp)
```

You’ll notice that all the values got rounded up: ### Method 3: Round down – Single DataFrame column

If you need to round the values down, you can then use the third method:

`df['DataFrame Column'].apply(np.floor)`

For our example:

`df['Value'].apply(np.floor)`

And this is the full Python code to round the values down using numpy:

```from pandas import DataFrame
import numpy as np

Sample = {'Value': [5.52132,6.572935,7.21,8.755,9.9989]}
df = DataFrame(Sample, columns= ['Value'])

roundDown = df['Value'].apply(np.floor)
print (roundDown)
```

Run the Python code, and you’ll get: So far, you’ve seen how to round values for a single DataFrame column.

But what if you’d like to round values across an entire DataFrame that contains multiple columns?

To accomplish this goal, you can use the fourth method below.

### Method 4: Round to specific decimals places – Entire DataFrame

Suppose that you have a new dataset with multiple columns:

 Values_1 Values_2 Text 5.52132 22.7352 AAA 6.572935 11.82 ABC 7.21 23.75839 XYZ 8.755 4.22 AABB 9.9989 15.1173 PPPP

This is how the DataFrame would look like in Python:

```from pandas import DataFrame

Sample = {'Values_1': [5.52132,6.572935,7.21,8.755,9.9989],
'Values_2': [22.7352,11.82,23.75839,4.22,15.1173],
'Text': ['AAA','ABC','XYZ','AABB','PPPP']
}

df = DataFrame(Sample, columns= ['Values_1','Values_2','Text'])
print (df)
```

Once you run the code in Python, you’ll get: Let’s say that your goal is to round the values to 2 decimals places across all the columns that contain numeric values (i.e., the Values_1 and Values_2 columns).

You can then use the fourth method to round the values for the entire DataFrame (for all the columns that contain numeric values):

`df.round(decimals=number of decimal places needed)`

And this is the code that you can use for our example:

```from pandas import DataFrame

Sample = {'Values_1': [5.52132,6.572935,7.21,8.755,9.9989],
'Values_2': [22.7352,11.82,23.75839,4.22,15.1173],
'Text': ['AAA','ABC','XYZ','AABB','PPPP']
}

df = DataFrame(Sample, columns= ['Values_1','Values_2','Text'])

roundTwoDecimals = df.round(decimals=2)
print (roundTwoDecimals)
```

You’ll see that the values are now rounded to 2 decimal places across the 2 columns that contain the numeric data: Alternatively, you can get the same results by using numpy:

`np.round(df, decimals=number of decimal places needed)`

So the complete Python code would look like this:

```from pandas import DataFrame
import numpy as np

Sample = {'Values_1': [5.52132,6.572935,7.21,8.755,9.9989],
'Values_2': [22.7352,11.82,23.75839,4.22,15.1173],
'Text': ['AAA','ABC','XYZ','AABB','PPPP']
}

df = DataFrame(Sample, columns= ['Values_1','Values_2','Text'])

roundTwoDecimals = np.round(df, decimals=2)
print (roundTwoDecimals)
```

You’ll get the same results using numpy: 