Davi Alefe
Davi Alefe

Reputation: 125

Maximum in two columns dataframe python pandas

I have two columns in a dataframe, one of them are strings (country's) and the other are integers related to each country. How do I ask which country has the biggest value using python pandas?

Upvotes: 0

Views: 675

Answers (1)

piRSquared
piRSquared

Reputation: 294586

Setup

df = pd.DataFrame(dict(Num=[*map(int, '352741845')], Country=[*'ABCDEFGHI']))

df

   Num Country
0    3       A
1    5       B
2    2       C
3    7       D
4    4       E
5    1       F
6    8       G
7    4       H
8    5       I

idxmax

df.loc[[df.Num.idxmax()]]

   Num Country
6    8       G

nlargest

df.nlargest(1, columns=['Num'])

   Num Country
6    8       G

sort_values and tail

df.sort_values('Num').tail(1)

   Num Country
6    8       G

Upvotes: 1

Related Questions