Skip to main content

np.round()

np.round(arr, decimals)

Rounds each element of an array to a specified number of decimal places.

Input:
arr : array_like
Input data, array-like structure containing numerical data to be rounded.
decimals : int, optional (default=0)
Number of decimal places to round to. If omitted, defaults to 0, meaning rounding to the nearest integer.
Returns:
rounded_array - An array of the same shape as a, with each element rounded to the specified number of decimal places.
Return Type:
ndarray
A new array containing the rounded values.

weight_ser = pets.get('Weight')
weight_ser
  • dog_00140
  • cat_00115
  • cat_00220
  • dog_00280
  • dog_00325
  • ham_0011
  • ham_0020.25
weight_arr = np.array(weight_ser)
weight_arr

array([40. , 15. , 20. , 80. , 25. , 1. , 0.25])

np.round(weight_arr, 0) # Doesn't include decimals

array([40., 2., 15., 80., 25., 1., 0., 10.])

np.round(weight_arr, 1) # Rounds to the nearest tenth - notice what happens to 0.25!

array([40. , 1.5, 15. , 80. , 25. , 1. , 0.2, 10. ])