数据框架:根据标准查找最低价格



我有一个csv文件,里面有笔记本电脑和规格的列表。我想找到屏幕大于15英寸的笔记本电脑的最低价格。我的代码出了什么问题?

laptops_15 = laptops_cleaned.loc[(laptops_cleaned['screen_size_inches'] > 15), laptops_cleaned['price_euros'].min()]
print(laptops_15)
#TypeError: cannot compare a dtyped [bool] array with a scalar of type [bool]

我不明白为什么Python不采用所有True值的最小价格?

您能提供一个数据示例吗?

我试图重现你的情况,我得到的错误信息是以下

TypeError: cannot do label indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [800] of <class 'numpy.int64'>

这是意料之中的,因为您使用的是price值作为索引,其中loc期望您在数据帧中使用的任何值作为索引(如果未指定,则为int(。我相信你正在努力实现的目标:

laptops_cleaned[laptops_cleaned['screen_size_inches'] > 15]['price_euros'].idxmin()

它为您提供了表条目的索引,使得列"price_euros"在条目中最小,从而使得列"screen_size_inches"大于15。

要获得最小值而不是最小索引,请使用min()方法而不是idxmin()

laptops_cleaned[laptops_cleaned['screen_size_inches'] > 15]['price_euros'].min()

试试这个:

laptops_15 = laptops_cleaned['price_euros'][laptops_cleaned['screen_size_inches'] > 15].min()

最新更新