After rounding to an integer the result of operations between lists that produce an array is there a way to remove the decimal point? I am using python in Jupyter notebooks.
Should I use something other than 'np.round'?
'FoodSpent and 'Income' and are simply two lists of data that I created. The initial rounding attempt left the decimal point.
>>>PercentFood = np.around((FoodSpent / Income) * 100, 0)
>>>PercentFood
array([[ 10., 7., 11., 10., 6., 10., 10., 12., 11., 9., 11.,
14.]
Thanks to advice given I ran the following, which rounded down to the integer without giving the decimal point.
>>> PercentFood = ((FoodSpent / Income) * 100)
>>> PercentFood.astype(int)
array([[ 9, 6, 11, 9, 6, 9, 10, 11, 10, 9, 11, 13]])
The answer suggested by the OP performs a floor operation (rounding "down" to the nearest integer). For more typical rounding use hpaulj's suggestion:
import numpy as np
foo = [1.1, 1.9, 3.4, 3.5]
bar = np.rint(foo).astype(int)
print(bar)
Which results in:
[1, 2, 3, 4]
I'm not sure how exactly your code works with this much context, but you can put this after rounding to get rid of the decimal.
PercentFood = [round(x) for x in PercentFood]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With