我在名为example的对象的列中有很多分数。我想将这些分数分成十分位数,并为每行分配相应的十分位间隔。我尝试了以下方法:

import random
import pandas as pd
random.seed(420) #blazeit
example = pd.DataFrame({"Score":[random.randrange(350, 1000) for i in range(1000)]})
example["Decile"] = pd.qcut(example["Score"], 10, labels=False) + 1 # Deciles as integer from 1 to 10
example["Decile_interval"] = pd.qcut(example["Score"], 10) # Decile as interval


这给了我我要寻找的东西。但是,我希望example["Decile_interval"]中的十分位数是整数,而不是浮点数。我尝试了precision=0,但它只是在每个数字的末尾显示.0

如何将区间中的浮点数转换为整数?

编辑:@ALollz指出,这样做将改变十分位数的分布。但是,我这样做只是出于演示目的,因此我对此并不担心。支持@JuanC来实现这一点并发布一个解决方案。

最佳答案

这是我使用简单的apply函数的解决方案:

example["Decile_interval"] = example["Decile_interval"].apply(lambda x: pd.Interval(left=int(round(x.left)), right=int(round(x.right))))

关于python - 从 Pandas qcut间隔中删除小数点(将间隔转换为整数),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57856740/

10-14 19:34