假设我有一个csv文件,其中包含“名称”,“价格”和“颜色”三列。
我该如何获取最昂贵的蓝色物品,最昂贵的红色和最昂贵的黄色之类的名字的变量?
非常感谢您的帮助:)
我们的计划是找到所需的类(例如“蓝色”项目),然后找到最贵的类(price
列中的最大值)。
让我们定义一个示例DataFrame:
import pandas as pd
df = pd.DataFrame({
'name': [a for a in "abcdef"],
'price': [1.5, 3.8, 1.4, 5.9, 3.5, 1.9],
'color': ['blue', 'red', 'yellow', 'blue', 'red', 'yellow']
}).set_index('name')
这是我们的DataFrame:
price color
name
a 1.5 blue
b 3.8 red
c 1.4 yellow
d 5.9 blue
e 3.5 red
f 1.9 yellow
要进行第一部分(查找特定颜色的项目),我们可以使用Pandas' query
。因此,以下将选择蓝色项目并保存到blue_items
。
blue_items = df[df.color == "blue"] # selects the df's slice in which df.color is equals to "blue".
然后,我们可以获得最高价格的指数(因为我已定义name
为指数列,它将返回名称):
blue_items["price"].idxmax()
完整的代码(现在考虑要导入CSV文件):
import pandas as pd
df = pd.read_csv("filename.csv", index_col="name")
most_exp_blue = df[df.color == "blue"]["price"].idxmax() # the most expensive blue
most_exp_red = df[df.color == "red"]["price"].idxmax() # the most expensive red
most_exp_yellow = df[df.color == "yellow"]["price"].idxmax() # the most expensive yellow
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句