Home > database >  pandas returning line number and type?
pandas returning line number and type?

Time:12-01

I have a csv file, and using python get the highest average price of avocado from the data. All works fine until printing the region

avocadoesDB = pd.read_csv("avocado.csv")
avocadoesDB = pd.DataFrame(avocadoesDB)
avocadoesDB = avocadoesDB[['AveragePrice', 'type', 'year', 'region']]



regions = avocadoesDB[['AveragePrice', 'region']]
regionMax = max(regions['AveragePrice'])
region = regions.loc[regions['AveragePrice']==regionMax]


print(f"The highest average price for both types of potatoes is ${regionMax} from {region['region']}.")

Output:

The highest average price for both types of potatoes is $3.25 from 14125    SanFrancisco
Name: region, dtype: object.

Expected:

The highest average price for both types of potatoes is $3.25 from SanFrancisco.

CodePudding user response:

So i've tried to copy the similar method on a simple dataset and i've seem to make it work, here's the code snippet

mx = max(df1['Salary'])
plc = df.loc[df1['Salary']==mx]['Name']
print('Max Sal : '   str(plc.iloc[0]))

Output:

Max Sal : Farah

According to this post on Stack Overflow, when you use df.loc[df1['Salary']==mx]['Name'] , A Series Object is returned, and so to retrieve the value of the desired column, you use [0], if I understood the post correctly.

So for your code, you can replace

region = regions.loc[regions['AveragePrice']==regionMax]
print(f"The highest average price for both types of potatoes is ${regionMax} from {region['region']}.")

with

region = regions.loc[regions['AveragePrice']==regionMax]['region']

print(f"The highest average price for both types of potatoes is ${regionMax} from {region}.")

This should work. Hope this helps!

  • Related