# 自定义分组规则
def get_score_group(score):
if score <= 4:
score_group = 'low'
elif score <= 6:
score_group = 'middle'
else:
score_group = 'high'
return score_group
# 方法1:传入自定义的函数进行分组按单列分组
data2 = data.set_index('Happiness Score')
data2.groupby(get_score_group).size()
# 方法2:人为构造出一个分组列
data['score group'] = data['Happiness Score'].apply(get_score_group)
data.tail()
data.groupby('score group').size(