[英]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所以我无法理解这里的 index[0] 的含义是什么,以及与方法 1 相比,它如何仅给出字符串格式的索引(我想要作为输出)。另外,当我使用 index[1] 时我收到错误:IndexError: index 1 is out of bounds for axis 0 with size 1
Can somebody please clarify the meaning of index[0] here?有人可以在这里澄清 index[0] 的含义吗?
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]
.当您调用
.index
时,它会返回索引 object,其中唯一的元素'Brazil'
可通过在.index[0]
0 处索引索引 object 来访问,所以
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.当您调用
.index[1]
时,您试图访问索引 object 的第二个元素,而它只包含一个元素,因此出现错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.