april
april

Reputation: 73

How to find the highest value of a specified group using Python

In the example below, how do I find out the highest price of 'mansion' ?

Data Description

this is a csv dataset contains three columns:h_type,h_price,y_year. Under the first column h_type, there are two different types of house, (mansion and apartment). The row is a list of a transaction.

Usage

I want to be able to implement a code so when an end user request the price via linebot, it will automatically provide information.

since there are tow types of house(mension and apartment), instead of finding a highest price of the whole data, i would like to narrow down to a specified type.

bond=pd.read_csv('/content/.., index_col='h_type')

if bond.loc['mansion']:      #<= how?,bad code here
  idMax = priceSr.idxmax()
  if  not isnan(idMax): 
    maxSr = df.loc[idMax]
    if most is None:
      most = maxSr.copy()
    else:
      if float(maxSr['h_price']) > float(most['h_price']):
        most = maxSr.copy()

most = most.to_frame().transpose()
print(most, '\n==========') 

This is the error message

   1536     def __nonzero__(self):
   1537         raise ValueError(
-> 1538             f"The truth value of a {type(self).__name__} is ambiguous. "
   1539             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
   1540         )

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

However, when i try this, it works

bond=pd.read_csv('/content/...,index_col='h_type')
a=bond.loc['mansion']
aMax=a.sort_values(['h_price'],ascending=False).head(1)

please give me the advise to modify the bad code up there!

thank you!

Upvotes: 1

Views: 67

Answers (1)

Mansi Varshney
Mansi Varshney

Reputation: 31

The built-in min() and max() have two different signatures that allow you to call them either with an iterable as their first argument or with two or more regular arguments. The signature that accepts a single iterable argument looks something like this:

min(iterable, *[, default, key]) -> minimum_value
max(iterable, *[, default, key]) -> maximum_value

Upvotes: 2

Related Questions