Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫,努比四舍五入到最接近的100只_Python_Pandas_Numpy - Fatal编程技术网

Python 熊猫,努比四舍五入到最接近的100只

Python 熊猫,努比四舍五入到最接近的100只,python,pandas,numpy,Python,Pandas,Numpy,我用下面的代码创建了一个dataframe列,并试图找出如何将其四舍五入到最接近的第100位 ... # This prints out my new value rounded to the nearest whole number. df['new_values'] = (10000/df['old_values']).apply(numpy.floor) # How do I get it to round down to the nearest 100th instead? #

我用下面的代码创建了一个dataframe列,并试图找出如何将其四舍五入到最接近的第100位

...
# This prints out my new value rounded to the nearest whole number.    
df['new_values'] = (10000/df['old_values']).apply(numpy.floor)

# How do I get it to round down to the nearest 100th instead?
# i.e. 8450 rounded to 8400

您需要除以
100
,转换为
int
,最后乘以
100

df['new_values'] = (df['old_values'] / 100).astype(int) *100
同:

df['new_values'] = (df['old_values'] / 100).apply(np.floor).astype(int) *100
样本:

df = pd.DataFrame({'old_values':[8450, 8470, 343, 573, 34543, 23999]})
df['new_values'] = (df['old_values'] / 100).astype(int) *100
print (df)
   old_values  new_values
0        8450        8400
1        8470        8400
2         343         300
3         573         500
4       34543       34500
5       23999       23900
编辑:


我用数学模块做过类似的尝试

a = [123, 456, 789, 145]

def rdl(x):
ls = []
for i in x:
    a = math.floor(i/100)*100
    ls.append(a)
return ls

rdl(a)
#输出为[100200400700100]


希望这能提供一些想法。它非常类似于@jezrael提供的解决方案,借用@jezrael的示例数据帧

df = pd.DataFrame({'old_values':[8450, 8470, 343, 573, 34543, 23999]})
使用
floordiv
/

df // 100 * 100

   old_values
0        8400
1        8400
2         300
3         500
4       34500
5       23900

您好@jezrael,谢谢您的回复!最初,我的
新值
是通过
10000/df['old\u values']
计算的。如果我换个角度看,我的计算就错了。你的意思是,在应用你的方法进行四舍五入之前,我需要先进行计算吗?我有点困惑,你是否认为
(df['old_values']/10000)
而不是
(10000/df['old_values'])
?是的,因此我的
新_值
是通过计算
10000/df['old_values']得到的。如果我做
df['old_values']/10000
,结果肯定会不同。类似于
2/3
3/2
的不同之处。希望我的解释有意义?我试过:
df['new_values']=((10000/df['old_values'])/100)。astype(int)*100
。当我打印出数据框时,
df['new_values']
它给了我想要的东西(至少基于前5行)超级,我还将其添加到答案中-只需更改示例。
df // 100 * 100

   old_values
0        8400
1        8400
2         300
3         500
4       34500
5       23900