Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

df.round Python Pandas

Tags:

python

pandas

Can someone give me a tip if its possible to round to the nearest integer value of 5 or 10.

My data looks like this:

            Date_Time    kW
0 2011-03-01 00:15:00  171.36
1 2011-03-01 00:30:00  181.44
2 2011-03-01 00:45:00  175.68
3 2011-03-01 01:00:00  180.00

And I am wanting to round the kW column to the nearest 5 or 10 kW value.

df = df.round({'rollingKw':0})

This gets me to the nearest integer but I would like to round to the nearest 5 or 10 kW to reduce the resolution in plotting my dataset.

like image 777
bbartling Avatar asked Feb 04 '26 08:02

bbartling


1 Answers

Check with this (floor round)

df.kW//5*5
Out[175]: 
0  2011-03-01    170.0
1  2011-03-01    180.0
2  2011-03-01    175.0
3  2011-03-01    180.0
Name: kW, dtype: float64

Or if you want the nearest

(df.kW/5).round().mul(5).astype(int)
0  2011-03-01    170
1  2011-03-01    180
2  2011-03-01    175
3  2011-03-01    180
like image 169
BENY Avatar answered Feb 06 '26 23:02

BENY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!