OgeiD
OgeiD

Reputation: 141

Selecting the row with the maximum value in a column in geopandas

I am using geopanda with python. I would like to select the row with the maximum value of column "pop".

I am trying the solution given here Find maximum value of a column and return the corresponding row values using Pandas :

city_join.loc[city_join['pop'].idxmax()]

However, it does not work. It says "TypeError: reduction operation 'argmax' not allowed for this dtype" . I think the reason is because that solution works for panda and not for geopandas. Am I right? How can I select the row with the maximun value of column "pop" in a geopanda dataframe?

Upvotes: 2

Views: 679

Answers (1)

ahmedshahriar
ahmedshahriar

Reputation: 1076

check your type with print(df['columnName'].dtype) and make sure it is numeric (i.e. integer, float ...). if it returns just object then use df['columnName'].astype(float) instead

Try with - city_join.loc[city_join['pop'].astype(float).idxmax()] if pop column is object type

Or

You can convert the column to numeric first

city_join['pop'] = pd.to_numeric(city_join['pop'])

and run your code city_join.loc[city_join['pop'].idxmax()]

Upvotes: 2

Related Questions