pandas - 如何将 pandas 列中的数字拆分为十分位数?

标签 pandas

我在 pandas 数据集中有一列随机值范围为 100 到 500 的列。

我需要从中创建一个新的“十分位数”列 - 就像排名一样,总共 20 个十分位数。我需要根据该值分配 20 级的排名。

  • 10 到 20 - 是第一个十分位数,数字 1
  • 20 到 30 - 是第二个十分位数,数字 2

      x = np.random.randint(100,501,size=(1000)) # column of 1000 rows with values ranging btw 100, 500.
    
    df['credit_score'] = x
    
        df['credit_decile_rank'] = df['credit_score'].map( lambda x: int(x/20) )
    
        df.head()
    

最佳答案

使用整数除以10:

df = pd.DataFrame({
         'credit_score':[4,15,24,55,77,81],
})

df['credit_decile_rank'] = df['credit_score'] // 10
print (df)
   credit_score  credit_decile_rank
0             4                   0
1            15                   1
2            24                   2
3            55                   5
4            77                   7
5            81                   8

关于pandas - 如何将 pandas 列中的数字拆分为十分位数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57904811/

相关文章:

python - 修补一个方法而不改变该方法的工作方式?

python - Pandas 如何从系列转换为数据框

python - 单个变量的频率表

python - 对值进行分组并检查另一列中的值是否在 python pandas dataframe 中相互抵消

python - 使用 groupby 获取组中具有最大值的行

python - Pandas 相当于 SQL where

python - 如何在 python 中对时间戳进行装箱并分配标签

python - 向 Pandas 中的字符串拆分命令添加函数

python - 过滤掉 Pandas 数据框中 A 列或 B 列上不存在的数字?

python - 从 pandas(cumpound) 的日返回率计算月返回率