Reputation:
Suppose we have a Dataframe:
df = pd.DataFrame({'population': [59000000, 65000000, 434000,
434000, 434000, 337000, 11300,
11300, 11300],
'GDP': [1937894, 2583560 , 12011, 4520, 12128,
17036, 182, 38, 311],
'alpha-2': ["IT", "FR", "MT", "MV", "BN",
"IS", "NR", "TV", "AI"]},
index=["Italy", "France", "Malta",
"Maldives", "Brunei", "Iceland",
"Nauru", "Tuvalu", "Anguilla"])
And I would like to find 3 countries where the population is the highest. Normally I would use
df.nlargest(3, 'population')
And it would print me
France 65000000 2583560 FR
Italy 59000000 1937894 IT
Malta 434000 12011 MT
How to modify this code to show me only for example Countries or alpha-2 not all the columns?
Upvotes: 2
Views: 585
Reputation: 13821
Since countries are your index
, you can use the same code and just grab the index:
df.nlargest(3, 'population').index
Get's you:
Index(['France', 'Italy', 'Malta'], dtype='object')
If your 'countries' is your 2nd column, you can print it with iloc
at index position 1 (python indexes start from 0, so 1 means essentially your 2nd column:
df.nlargest(3, 'population').iloc[:,1]
Out[60]:
1 France
0 Italy
2 Malta
Or you can use loc
:
df.nlargest(3, 'population').loc[:,'countries']
Out[64]:
1 France
0 Italy
2 Malta
Name: index, dtype: object
Upvotes: 1
Reputation: 31166
It's just the contents of the index...
df.nlargest(3, 'population').index.values
array(['France', 'Italy', 'Malta'], dtype=object)
Upvotes: 0
Reputation: 1068
Try this
df.nlargest(3, 'population').index.values
Will print
['France' 'Italy' 'Malta']
Upvotes: 0