我有以下数组:
array.unique()
array(['10','8', '15','20','21','22 '27','28' nan, '30', '32', '33', 'Values']我正在尝试分配以下类别标签,并将它们放在相应的bin中:'not_number','10及以下‘,'11'- '32','33’及以上,使用pd.cut(),结果应该是:
'not_number' 2 <= this includes ('Values', nan)
'10 and below' 2
'11'- '32' 9
'33 and up' 1发布于 2022-05-27 21:09:35
如果传递非int数组数据类型,则剪切方法将引发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())https://stackoverflow.com/questions/72410579
复制相似问题