简体   繁体   中英

How to find index in a string format for a particular value in a column of a pandas dataframe?

I have a dataframe named Top15 like this:  
Country              % Renewable Energy   Rank
Brazil               69.65                15
Canada               61.95                56
China                19.75                32
Germany              17.90                 2
Spain                37.97                11
France               17.02                12
United Kingdom       10.60                 5
India                14.97                10
Iran                  5.71                21
......                ......             .....

United States        11.57                38

Here i want to find the Country with greatest %Renewable Energy. 
Here country is the index of dataframe and hence i want the name of index only in string format.
But when i use the following code:
maximum=Top15['% Renewable'].max()
Top15[Top15['% Renewable']==maximum].index       (Method 1)
I get :Index(['Brazil'], dtype='object', name='Country')  

But when i use: Top15[Top15['% Renewable']==maximum].index[0] (Method 2)
I get:'Brazil'

So i am unable to understand that what is the meaning of index[0] here and how it is giving only the index in a string format(which i want as output) compared to method 1. Also when i am using index[1] i am getting error: IndexError: index 1 is out of bounds for axis 0 with size 1

Can somebody please clarify the meaning of index[0] here?

When you are calling .index it returns you the index object, the only element of which, 'Brazil' , is accessible via indexing the index object at position 0, so .index[0] .

When you are calling .index[1] you are trying to access the second element of the index object while it only contains one element, hence the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM