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

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

Rounding to the closest bin in pandas/numpy

问题

  1. import numpy as np
  2. import pandas as pd
  3. binsize = 0.05
  4. df = pd.DataFrame()
  5. df['distance'] = [0.01555, 0.6, 0.99, 1.24]
  6. min_rounded = np.round(df['distance'].min() / binsize) * binsize
  7. max_rounded = np.round(df['distance'].max() / binsize) * binsize
  8. min_rounded, max_rounded

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

英文:

I have dataframe containing distances as float64 values.

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

This returns:

  1. distance
  2. 0 0.01555
  3. 1 0.60000
  4. 2 0.99000
  5. 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:

  1. np.round(np.round(df['distance'].min() / binsize) * binsize, 2)
  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

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

  1. def quick_round(binsize, item):
  2. return (((item + binsize / 2) * (1 / binsize)) // 1) * binsize
  3. print(quick_round(binsize, df['distance'].min()), quick_round(binsize, df['distance'].max()))

结果为:

  1. 0.0 1.25
英文:

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

  1. def quick_round(binsize, item):
  2. return (((item + binsize / 2) * (1 / binsize)) // 1) * binsize
  3. print(quick_round(binsize,df['distance'].min()), quick_round(binsize,df['distance'].max()))

Yields:

  1. 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:

确定