如何基于另一个列中的值在pandas数据框列中创建新值 [英] How to create new values in a pandas dataframe column based on values from another column
问题描述
我有一个从csv文件读取的值的pandas数据框.我有一列标记为"SleepQuality"的值从0.0-100.0浮动.我想创建一个新标签为'SleepQualityGroup'的列,其中原始列btw 0-49中的值在新列中的值为0,50-59 = 1,60-69 = 2,70-79 = 3,80 -89 = 4,和90-100 = 5
I have a pandas dataframe of values I read in from a csv file. I have a column labeled 'SleepQuality' and the values are float from 0.0 - 100.0. I want to create a new column labeled 'SleepQualityGroup' where values from the original column btw 0 - 49 have a value of 0 in the new column, 50 - 59 = 1 , 60 - 69 = 2, 70 - 79 = 3, 80 - 89 = 4, and 90 - 100 = 5
要执行此操作,最好的公式是什么?我被困在确定每个范围内的所有值并分配给新值所需的逻辑上.
What would be the best formula to use in order to do this? I am stuck on the logic needed to identify all values in each range and assign to the new value.
下面在新的"SleepQualityGroup"列中显示的输出示例.
An example of what the output would like like below in the new 'SleepQualityGroup' column.
SleepQuality SleepQualityGroup
80.4 4
90.1 5
66.4 2
50.3 1
86.2 4
75.4 3
45.7 0
91.5 5
61.3 2
54 1
58.2 1
推荐答案
使用pd.cut
即
df['new'] = pd.cut(df['SleepQuality'],bins=[0,50 , 60, 70 , 80 , 90,100], labels=[0,1,2,3,4,5])
输出:
SleepQuality SleepQualityGroup new
0 80.4 4 4
1 90.1 5 5
2 66.4 2 2
3 50.3 1 1
4 86.2 4 4
5 75.4 3 3
6 45.7 0 0
7 91.5 5 5
8 61.3 2 2
9 54.0 1 1
10 58.2 1 1
这篇关于如何基于另一个列中的值在pandas数据框列中创建新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!