对系列进行装箱将返回看似无关的TypeError

时间:2018-11-20 19:09:03

标签: python pandas jupyter

我正在尝试将我创建的数据帧切成垃圾箱:

picture of dataframe in case it's relevant

# create bins and labels
bins = [575, 600, 625, 650]
labels = [
    "$575-$599",
    "$600-$624",
    "$625-$649",
    "$650-$675"
]

schoolSummary["Spending Range"] = pd.cut(schoolSummary["Per Student Budget"], bins, labels = labels)

由于某种原因,我收到此错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-73-b938397739fa> in <module>()
      9 
     10 #schoolSummary["Spending Range"] =
---> 11 pd.cut(schoolSummary["Per Student Budget"], bins, labels = labels)

~\Anaconda3\envs\py36\lib\site-packages\pandas\core\reshape\tile.py in cut(x, bins, right, labels, retbins, precision, include_lowest, duplicates)
    232                               include_lowest=include_lowest,
    233                               dtype=dtype,
--> 234                               duplicates=duplicates)
    235 
    236     return _postprocess_for_cut(fac, bins, retbins, x_is_series,

~\Anaconda3\envs\py36\lib\site-packages\pandas\core\reshape\tile.py in _bins_to_cuts(x, bins, right, labels, precision, include_lowest, dtype, duplicates)
    335 
    336     side = 'left' if right else 'right'
--> 337     ids = _ensure_int64(bins.searchsorted(x, side=side))
    338 
    339     if include_lowest:

TypeError: '<' not supported between instances of 'int' and 'str'

我很困惑,因为我根本没有在代码中使用“ <”。我也用过

print(type(schoolSummary["Per Student Budget"]))

,它是一个系列对象,所以我不知道它指的是“ int”和“ str”。我的垃圾箱或标签有问题吗?

1 个答案:

答案 0 :(得分:0)

由于答辩率低,我无法评论您的问题,

您必须尝试以下操作

Dim EmployeeID As Integer = cbxEmployee.SelectedValue

因为bins = [575, 600, 625, 650] labels = [ "$575-$599", "$600-$624", "$625-$649", "$650-$675" ] for bin_ in bins: schoolSummary["Spending Range"] = pd.cut(schoolSummary["Per Student Budget"], bin_, labels = labels) 的类型为bin,而不是int