簡體   English   中英

如何在 pandas dataframe 的列中以字符串格式查找特定值的索引?

[英]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'

所以我無法理解這里的 index[0] 的含義是什么,以及與方法 1 相比,它如何僅給出字符串格式的索引(我想要作為輸出)。另外,當我使用 index[1] 時我收到錯誤:IndexError: index 1 is out of bounds for axis 0 with size 1

有人可以在這里澄清 index[0] 的含義嗎?

當您調用.index時,它會返回索引 object,其中唯一的元素'Brazil'可通過在.index[0] 0 處索引索引 object 來訪問,所以

當您調用.index[1]時,您試圖訪問索引 object 的第二個元素,而它只包含一個元素,因此出現錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM