ROUND

11/13/16

The ROUND function rounds a number to a specified number of digits.

In [1]:
import pandas as pd
In [2]:
df = pd.read_csv('sample_data.csv')
In [3]:
df
Out[3]:
Name Date Item Product Category Time (hrs) Amt Picked (lbs) Sale Value ($/lb)
0 Lucas 1/5/16 Green Apples Apples 2 5 2.5
1 Valter 1/5/16 European Pear Pears 4 10 2.0
2 Erik 1/8/16 Red Apples Apples 2 3 2.1
3 Georg 2/1/16 Asian Pear Pears 8 15 3.8
4 Lucas 2/10/16 Red Apples Apples 6 6 3.6

Round a computed value

NB: in Python programming, a scalar is simply a variable that holds one individual value. This is in contrast to a variable that holds multiple values, such as a list, dict, array, etc.

Rounding a series

In [37]:
arbitrary_scalar = 15.11115555
computed_series = df['Time (hrs)']*arbitrary_scalar
In [38]:
computed_series
Out[38]:
0     30.222311
1     60.444622
2     30.222311
3    120.889244
4     90.666933
Name: Time (hrs), dtype: float64
In [26]:
computed_series.round()
Out[26]:
0     30
1     60
2     30
3    121
4     91
Name: Time (hrs), dtype: float64
In [27]:
computed_series.round(1)
Out[27]:
0     30.2
1     60.4
2     30.2
3    120.9
4     90.7
Name: Time (hrs), dtype: float64
In [28]:
computed_series.round(2)
Out[28]:
0     30.22
1     60.44
2     30.22
3    120.89
4     90.67
Name: Time (hrs), dtype: float64

Rounding a dataframe

In [39]:
computed_df = df[['Time (hrs)', 'Amt Picked (lbs)']]*arbitrary_scalar
In [40]:
computed_df
Out[40]:
Time (hrs) Amt Picked (lbs)
0 30.222311 75.555778
1 60.444622 151.111556
2 30.222311 45.333467
3 120.889244 226.667333
4 90.666933 90.666933
In [41]:
computed_df.round()
Out[41]:
Time (hrs) Amt Picked (lbs)
0 30 76
1 60 151
2 30 45
3 121 227
4 91 91
In [42]:
computed_df.round(1)
Out[42]:
Time (hrs) Amt Picked (lbs)
0 30.2 75.6
1 60.4 151.1
2 30.2 45.3
3 120.9 226.7
4 90.7 90.7
In [43]:
computed_df.round(5)
Out[43]:
Time (hrs) Amt Picked (lbs)
0 30.22231 75.55578
1 60.44462 151.11156
2 30.22231 45.33347
3 120.88924 226.66733
4 90.66693 90.66693
In [ ]: