Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding up a column

Tags:

python

pandas

I am new to pandas python and I am having difficulties trying to round up all the values in the column. For example,

Example
88.9
88.1
90.2
45.1

I tried using my current code below, but it gave me:

AttributeError: 'str' object has no attribute 'rint'

 df.Example = df.Example.round()
like image 253
Jason Ching Yuk Avatar asked Aug 11 '16 10:08

Jason Ching Yuk


People also ask

How do I round up an entire column in Excel?

Click the cell where you want your rounded result to go. Head to Formulas > Math & Trig, and then choose either the “ROUNDUP” or “ROUNDDOWN” function from the dropdown menu. Enter the number (or cell) you want to round in the “Number” field.

How do you make Excel round up or down?

To always round up (away from zero), use the ROUNDUP function. To always round down (toward zero), use the ROUNDDOWN function. To round a number to a specific multiple (for example, to round to the nearest 0.5), use the MROUND function.


1 Answers

You can use numpy.ceil:

In [80]: import numpy as np

In [81]: np.ceil(df.Example)
Out[81]: 
0    89.0
1    89.0
2    91.0
3    46.0
Name: Example, dtype: float64

depending on what you like, you could also change the type:

In [82]: np.ceil(df.Example).astype(int)
Out[82]: 
0    89
1    89
2    91
3    46
Name: Example, dtype: int64

Edit

Your error message indicates you're trying just to round (not necessarily up), but are having a type problem. You can solve it like so:

In [84]: df.Example.astype(float).round()
Out[84]: 
0    89.0
1    88.0
2    90.0
3    45.0
Name: Example, dtype: float64

Here, too, you can cast at the end to an integer type:

In [85]: df.Example.astype(float).round().astype(int)
Out[85]: 
0    89
1    88
2    90
3    45
Name: Example, dtype: int64
like image 72
Ami Tavory Avatar answered Nov 15 '22 01:11

Ami Tavory