将一个序列装箱返回一个看似不相关的类型

2024-09-27 00:20:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将我创建的数据帧切片到存储箱中:

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"]))

它是一个series对象,所以我不知道它指的是什么int和str。我的箱子或标签有问题吗?你知道吗


Tags: ofinrightlabelsincludestudentsideduplicates
1条回答
网友
1楼 · 发布于 2024-09-27 00:20:59

由于代表性低,我不能对你的问题发表评论

你必须尝试以下方法

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类型,而不是list。你知道吗

相关问题 更多 >

    热门问题