栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

pandas如何使用pd.cut()

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pandas如何使用pd.cut()

test[‘range’] = pd.cut(test.days, [0,30,60], include_lowest=True)
print (test)
daysrange
0 0 (-0.001, 30.0]
1 31 (30.0, 60.0]
2 45 (30.0, 60.0]


看区别:

test = pd.Dataframe({'days': [0,20,30,31,45,60]})test['range1'] = pd.cut(test.days, [0,30,60], include_lowest=True)#30 value is in [30, 60) grouptest['range2'] = pd.cut(test.days, [0,30,60], right=False)#30 value is in (0, 30] grouptest['range3'] = pd.cut(test.days, [0,30,60])print (test)   days          range1    range2    range30     0  (-0.001, 30.0]   [0, 30)       NaN1    20  (-0.001, 30.0]   [0, 30)   (0, 30]2    30  (-0.001, 30.0]  [30, 60)   (0, 30]3    31    (30.0, 60.0]  [30, 60)  (30, 60]4    45    (30.0, 60.0]  [30, 60)  (30, 60]5    60    (30.0, 60.0]       NaN  (30, 60]

或使用

numpy.searchsorted
,但
days
hast的值必须排序:

arr = np.array([0,30,60])test['range1'] = arr.searchsorted(test.days)test['range2'] = arr.searchsorted(test.days, side='right') - 1print (test)   days  range1  range20     0       0       01    20       1       02    30       1       13    31       2       14    45       2       15    60       2       2


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/455723.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号