简体   繁体   English

Map 一列中的数值到另一列中的分类值 pandas

[英]Map numerical values in one column to categorical values in another column in pandas

I have a dataframe with numerical values我有一个带有数值的 dataframe

score分数
16.0 16.0
49.0 49.0
55.0 55.0
65.0 65.0
77.0 77.0
89.0 89.0
98.0 98.0

I want to create another column in the same dataframe with categorical values based on the numerical values.我想在同一个 dataframe 中创建另一列,其中包含基于数值的分类值。

score分数 names名字
16.0 16.0 low低的
49.0 49.0 low低的
55.0 55.0 low低的
65.0 65.0 avg平均值
77.0 77.0 avg平均值
89.0 89.0 high高的
98.0 98.0 very high很高
scores = df['score'].tolist()
names = []
for i in range(len(scores)):
    if scores[i] < 60.0:
        names.append('low')
    elif scores[i] < 80.0:
        names.append('avg')
    elif scores[i] < 90.0:
        names.append('high')
    else:
        names.append('very high')
df['names'] = names

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM