需要显示最高价格和拥有它的公司



所以,我刚开始使用python,我需要显示最高价格和拥有它的公司。我从一个CSV文件中获取数据,该文件包含描述某些汽车的多个列。我只对其中两个感兴趣:价格和公司。

我需要显示最高价格和拥有它的公司。一些建议? 这就是我尝试过的,我不知道如何获得公司,而不仅仅是最高价格。

import pandas as pd
df = pd.read_csv("Automobile_data.csv")
for x in df['price']:
if x == df['price'].max():
print(x)

使用Series.max,按DataFrame.set_index创建索引,按Series.idxmax获取company名称:

df = pd.DataFrame({
'company':list('abcdef'),
'price':[7,8,9,4,2,3],
})
print (df)
company  price
0       a      7
1       b      8
2       c      9
3       d      4
4       e      2
5       f      3
print(df['price'].max())
9
print(df.set_index('company')['price'].idxmax())
c

另一个想法是使用DataFrame.agg

s = df.set_index('company')['price'].agg(['max','idxmax'])
print (s['max'])
9
print (s['idxmax'])
c

如果可能的话,复制最大值,需要所有最高价格的公司使用boolean indexingDataFrame.loc- 获取Series

df = pd.DataFrame({
'company':list('abcdef'),
'price':[7,8,9,4,2,9],
})
print (df)
company  price
0       a      7
1       b      8
2       c      9
3       d      4
4       e      2
5       f      9
print(df['price'].max())
9
#only first value
print(df.set_index('company')['price'].idxmax())
c
#all maximum values
s = df.loc[df['price'] == df['price'].max(), 'company']
print (s)
2    c
5    f
Name: company, dtype: object

如果需要一行数据帧:

out = df.loc[df['price'] == df['price'].max(), ['company','price']]
print (out)
company  price
2       c      9

out = df.loc[df['price'] == df['price'].max(), ['company','price']]
print (out)
company  price
2       c      9
5       f      9

这就是不使用熊猫的方法。熊猫是为了避免循环

import pandas as pd
df = pd.read_csv("Automobile_data.csv")
max_price = df[df['price'] == df['price'].max()]
print(max_price)

这就是你会怎么做的。如果你只想要价格和公司

print(max_price[['company','price']])

说明:我们创建一个布尔过滤器,如果价格等于最高价格,则为 true。我们用这个作为面具来捕捉我们需要的东西。

除了Jezrael的完整答案外,我建议使用groupby如下:

df = pd.DataFrame({
'company':list('abcdef'),
'price':[7,8,9,4,2,3],
})
sorted_df = df.groupby(['price']).max().reset_index()
desired_row = sorted_df.loc[sorted_df.index[-1]]
price = desired_row[0]
company = desired_row[1]
print('Maximum price is: ', price)
print('The company is: ', company)

上面的代码打印:

Maximum price is:  9
The company is:  c

最新更新