Find maximum value of a column and return the corresponding row values using Pandas

PythonPandasDataframeMax

Python Problem Overview


Structure of data;

Using Python Pandas I am trying to find the Country & Place with the maximum value.

This returns the maximum value:

data.groupby(['Country','Place'])['Value'].max()

But how do I get the corresponding Country and Place name?

Python Solutions


Solution 1 - Python

Assuming df has a unique index, this gives the row with the maximum value:

In [34]: df.loc[df['Value'].idxmax()]
Out[34]: 
Country        US
Place      Kansas
Value         894
Name: 7

Note that idxmax returns index labels. So if the DataFrame has duplicates in the index, the label may not uniquely identify the row, so df.loc may return more than one row.

Therefore, if df does not have a unique index, you must make the index unique before proceeding as above. Depending on the DataFrame, sometimes you can use stack or set_index to make the index unique. Or, you can simply reset the index (so the rows become renumbered, starting at 0):

df = df.reset_index()

Solution 2 - Python

df[df['Value']==df['Value'].max()]

This will return the entire row with max value

Solution 3 - Python

I think the easiest way to return a row with the maximum value is by getting its index. argmax() can be used to return the index of the row with the largest value.

index = df.Value.argmax()
                                            

Now the index could be used to get the features for that particular row:

df.iloc[df.Value.argmax(), 0:2]

Solution 4 - Python

The country and place is the index of the series, if you don't need the index, you can set as_index=False:

df.groupby(['country','place'], as_index=False)['value'].max()

Edit:

It seems that you want the place with max value for every country, following code will do what you want:

df.groupby("country").apply(lambda df:df.irow(df.value.argmax()))

Solution 5 - Python

Use the index attribute of DataFrame. Note that I don't type all the rows in the example.

In [14]: df = data.groupby(['Country','Place'])['Value'].max()

In [15]: df.index
Out[15]: 
MultiIndex
[Spain  Manchester, UK     London    , US     Mchigan   ,        NewYork   ]

In [16]: df.index[0]
Out[16]: ('Spain', 'Manchester')

In [17]: df.index[1]
Out[17]: ('UK', 'London')

You can also get the value by that index:

In [21]: for index in df.index:
    print index, df[index]
   ....:      
('Spain', 'Manchester') 512
('UK', 'London') 778
('US', 'Mchigan') 854
('US', 'NewYork') 562

Edit

Sorry for misunderstanding what you want, try followings:

In [52]: s=data.max()

In [53]: print '%s, %s, %s' % (s['Country'], s['Place'], s['Value'])
US, NewYork, 854

Solution 6 - Python

In order to print the Country and Place with maximum value, use the following line of code.

print(df[['Country', 'Place']][df.Value == df.Value.max()])

Solution 7 - Python

You can use:

print(df[df['Value']==df['Value'].max()])

Solution 8 - Python

My solution for finding maximum values in columns:

df.ix[df.idxmax()]

, also minimum:

df.ix[df.idxmin()]

Solution 9 - Python

I'd recommend using nlargest for better performance and shorter code. import pandas

df[col_name].value_counts().nlargest(n=1)

Solution 10 - Python

import pandas
df is the data frame you create.

Use the command:

df1=df[['Country','Place']][df.Value == df['Value'].max()]

This will display the country and place whose value is maximum.

Solution 11 - Python

Using DataFrame.nlargest.

The dedicated method for this is nlargest which uses algorithm.SelectNFrame on the background, which is a performant way of doing: sort_values().head(n)

   x  y  a  b
0  1  2  a  x
1  2  4  b  x
2  3  6  c  y
3  4  1  a  z
4  5  2  b  z
5  6  3  c  z
df.nlargest(1, 'y')

   x  y  a  b
2  3  6  c  y

Solution 12 - Python

I encountered a similar error while trying to import data using pandas, The first column on my dataset had spaces before the start of the words. I removed the spaces and it worked like a charm!!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrichieView Question on Stackoverflow
Solution 1 - PythonunutbuView Answer on Stackoverflow
Solution 2 - PythonGauravView Answer on Stackoverflow
Solution 3 - Pythonsharad kakranView Answer on Stackoverflow
Solution 4 - PythonHYRYView Answer on Stackoverflow
Solution 5 - PythonwaitingkuoView Answer on Stackoverflow
Solution 6 - PythonArpit SharmaView Answer on Stackoverflow
Solution 7 - PythonkelvinkahuroView Answer on Stackoverflow
Solution 8 - PythonMarcin LentnerView Answer on Stackoverflow
Solution 9 - Pythonsaran3hView Answer on Stackoverflow
Solution 10 - PythonrakshaView Answer on Stackoverflow
Solution 11 - PythonErfanView Answer on Stackoverflow
Solution 12 - PythonJefferson SankaraView Answer on Stackoverflow