我在 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/