首页 > 解决方案 > df.round 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.

标签: pythonpandas

解决方案


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

推荐阅读