英文:
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.0
和1.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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论