繁体   English   中英

Python pandas.cut()

[英]Python pandas.cut()

我有以下数组:

array.unique()
array(['10','8', '15','20','21','22 '27','28' nan, '30', '32', '33', 'Values']

我正在尝试使用 pd.cut() 分配以下类别标签并将它们放在相应的 bin 中:'not_number'、'10 及以下'、'11'-'32'、'33 及以上'应该:

'not_number'        2   <= this includes ('Values', nan) 
'10 and below'      2 
'11'- '32'          9 
'33 and up'         1

如果您传递非 int 数组数据类型,cut 方法会引发 TypeError。 我建议的解决方案是从数组传递到列表来管理不同的数据类型。 在这种情况下,您可以使用列表推导将nan'Values'替换为负数。 有了这个集合,您可以在列表上使用 pd.cut 方法并标记数据。

a = np.array(['10','8', '15', '20','21','22', '27', '28', 'nan', '30', '32', '33', 'Value'])
a_list = [int(i) if i.isdigit() else -1 for i in c]
bins = pd.IntervalIndex.from_tuples([(-np.Inf, 0), (0, 10), (10, 32), (32, np.Inf)])
lab = ['Not a Value', '10 and below', '11 - 32', '33 and above']
a_cut = pd.cut(s, bins)
a_cut.categories = lab
print(a.value_counts())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM