2

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]])



chenderson
  • 35
  • 1
  • 5
  • Cast each to an `integer`. And fix your syntax. – Scott Hunter Jan 02 '20 at 19:42
  • 1
    How do I "cast each to an integer"? Thanks – chenderson Jan 02 '20 at 19:46
  • 2
    The decimal point indicates that the values are floating point. Look at `PercentFood.dtype`. `PercentFood.astype(int)` should cast your array to integer dtype. – hpaulj Jan 02 '20 at 20:15
  • Thanks...I tried `PercentFood.dtype` in the input and received this error" 'list' object has no attribute 'dtype'. I also tried `PercentFood.astype(int)` and it returned the same error message. Any thoughts on that @hpaulj? – chenderson Jan 03 '20 at 21:38
  • Then `PerentFood` is a list, not an array. What you show is an array, but apparently you are applying my suggestion(s) to something else. – hpaulj Jan 03 '20 at 21:44
  • It is an array. It seems that I had an error somewhere else in the program. When I tried your suggestion again it worked. Thanks @hpaulj – chenderson Jan 04 '20 at 02:32

1 Answers1

1

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]

GlerG
  • 65
  • 6
  • FoodSpent and Income are simply lists of 12 data points. I tried `for x in PercentFood: np.round(x)` and it did not clear the decimal. For the code you suggested I received this error message: This `type numpy.ndarray doesn't define __round__ method` – chenderson Jan 03 '20 at 21:45
  • Unfortunately I don’t know enough about numpy to provide further help. But I hope there is someone out there that does. – GlerG Jan 05 '20 at 06:50