0

Shown below are few details on a DataFrame.

enter image description here

Below is the syntax that is been used and do not get the expected output.

df = df.sort_values(by=['country','Year','Value'], ascending=[True,True,False])
df = df.drop_duplicates('country')

how could I get the expected output shown below

enter image description here

WF30
  • 213
  • 1
  • 3
  • 16

2 Answers2

3

Try sorting by "Value" and keeping the last row for each country

>>> df.sort_values("Value").drop_duplicates("country",keep="last")
    Year country  Value
2   2003     USA   7000
6   2002   India   9000
10  2001   Japan  10000

Alternatively, you could use groupby:

>>> df[df["Value"].eq(df.groupby("country")["Value"].transform('max'))]
    Year country  Value
2   2003     USA   7000
6   2002   India   9000
10  2001   Japan  10000
not_speshal
  • 22,093
  • 2
  • 15
  • 30
0

Try:

df.sort_values('Value',ascending=False).groupby('country').head(1)
rhug123
  • 7,893
  • 1
  • 9
  • 24