将最接近的箱进行四舍五入到 pandas/numpy 中

huangapple go评论66阅读模式
英文:

Rounding to the closest bin in pandas/numpy

问题

import numpy as np
import pandas as pd

binsize = 0.05
df = pd.DataFrame()
df['distance'] = [0.01555, 0.6, 0.99, 1.24]

min_rounded = np.round(df['distance'].min() / binsize) * binsize
max_rounded = np.round(df['distance'].max() / binsize) * binsize

min_rounded, max_rounded

这段代码用来将DataFrame中的距离值四舍五入到最接近binsize的倍数,对于上面的示例,它会返回0.01.25

英文:

I have dataframe containing distances as float64 values.

import numpy as np
import pandas as pd
binsize = 0.05
df = pd.DataFrame()
df['distance'] = [0.01555, 0.6, 0.99, 1.24] 

This returns:

 	distance
0 	0.01555
1 	0.60000
2 	0.99000
3 	1.24000

I would like to round the min and max values rounded to the closest multiple of binsize.

This is how I am currently doing this:

np.round(np.round(df['distance'].min() / binsize) * binsize, 2)
np.round(np.round(df['distance'].max() / binsize) * binsize, 2)

Thus returning 0.0 and 1.25 for the above example.

Is there an easier way to achieve this?

答案1

得分: 2

你可以使用一些古老的数值技巧,如果你想的话,这可以得到相同的结果:

def quick_round(binsize, item):
    return (((item + binsize / 2) * (1 / binsize)) // 1) * binsize

print(quick_round(binsize, df['distance'].min()), quick_round(binsize, df['distance'].max()))

结果为:

0.0 1.25
英文:

You can use some old numeric tricks if you want, this gets to the same place:

def quick_round(binsize, item):
    return (((item + binsize / 2) * (1 / binsize)) // 1) * binsize

print(quick_round(binsize,df['distance'].min()), quick_round(binsize,df['distance'].max()))

Yields:

0.0 1.25

huangapple
  • 本文由 发表于 2023年6月19日 01:00:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定