Pandas how to use pd.cut()

后端 未结 5 496
时光取名叫无心
时光取名叫无心 2020-12-02 17:28

Here is the snippet:

test = pd.DataFrame({\'days\': [0,31,45]})
test[\'range\'] = pd.cut(test.days, [0,30,60])

Output:

             


        
5条回答
  •  孤城傲影
    2020-12-02 17:42

    You can use labels to pd.cut() as well. The following example contains the grade of students in the range from 0-10. We're adding a new column called 'grade_cat' to categorize the grades.

    bins represent the intervals: 0-4 is one interval, 5-6 is one interval, and so on The corresponding labels are "poor", "normal", etc

    bins = [0, 4, 6, 10]
    labels = ["poor","normal","excellent"]
    student['grade_cat'] = pd.cut(student['grade'], bins=bins, labels=labels)
    

提交回复
热议问题