如何在python中选择特定的容器类别?

2024-03-29 12:14:54 发布

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

我有一个号码的清单,我用这些号码把它们分成几个箱子熊猫。切(). 如何选择一种垃圾箱?在

manhattanBedrmsPrice.head()

0      859
5     1055
9      615
11     663
13    1317
Name: Price Value, dtype: int64


bins = [400,600,800,1000,1200, 1400,1600,1800,2000,2200,2400,2600,2800,3000]

manPriceCategories = pd.cut(manhattanBedrmsPrice, bins)

我得到以下类别:

^{pr2}$

如何选择特定类别?在


Tags: namevalue类别pricehead号码pddtype
2条回答

对你来说

manPriceCategories.loc[manPriceCategories.isin([pd.Interval(600,800)])]

或者使用分类数据的代码

^{pr2}$

您可以将类别分配给一个变量(例如cats),然后使用布尔索引检查序列中的值是否等于感兴趣的类别。在

cats = manPriceCategories.cat.categories
>>> manPriceCategories.loc[manPriceCategories.eq(cats[1])]
9     (600, 800]
11    (600, 800]
Name: Price Value, dtype: category
Categories (13, interval[int64]): [(400, 600] < (600, 800] < (800, 1000] < (1000, 1200] ... (2200, 2400] < (2400, 2600] < (2600, 2800] < (2800, 3000]]
cats = manPriceCategories.cat.categories
cats = manPriceCategories.cat.categories

您可以使用字典理解来枚举您的类别,以便知道它们的索引位置(例如1for(600800])。在

^{pr2}$

相关问题 更多 >