繁体   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