如何在 pandas 中使用pd.cut [英] How to use pd.cut in pandas

查看:52
本文介绍了如何在 pandas 中使用pd.cut的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮我弄清楚为什么它不起作用:

Can anyone help me figure out why this isn't working:

ages = ['15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84']

race['age_group'] = pd.cut(race.Age,range(13,84,5),right=False, labels=ages)

race[['Age','age_group']].head(15)

这是我得到的结果:

        Age age_group
    0   31  30-34
    1   38  40-44
    2   45  45-49
    3   30  30-34
    4   45  45-49
    5   35  35-39
    6   32  30-34
    7   33  35-39
    8   29  30-34
    9   42  40-44
   10   34  35-39
   11   48  50-54
   12   35  35-39
   13   51  50-54
   14   38  40-44

推荐答案

您的范围"不正确,请尝试:

Your "range" is not correct, try:

ages = ['15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84']
race['age_group'] = pd.cut(race.Age,range(15,86,5),right=False, labels=ages)
race[['Age','age_group']].head(15)

这篇关于如何在 pandas 中使用pd.cut的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆