Reputation: 293
I have a Pandas dataframe (countries) and need to get specific index value. (Say index 2 => I need Japan)
I used iloc, but i got the data (7.542)
return countries.iloc[2]
7.542
Upvotes: 7
Views: 33693
Reputation: 460
This is exactly the question I had! Reading the other responses helped me find this answer.
As other answerers have mentioned, the presented structure of the table looks like you have a dataframe with two columns, one column for 'Country_Names' and another unnamed column for values, in which case the index would default to [0, 1 ... n].
But, your sample code return countries.iloc[2] #7.542
suggests you have a series since it only returns a scalar value, rather than a key:value pair with an index and a datatype (see below).
So, let's assume that you have a dataframe, as you say you do, with one column of values and 'Country_Names' as the index. I'll add a name to the values column and add a second values column:
countries = pd.DataFrame({'Country_Names': ['China', 'United States', 'Japan', 'United Kingdom', 'Russian Federation', 'Brazil'],
'Values1': [1.5, 10.53, 7.542, 3.487, 6.565, 8.189],
'Values2': [1,2,3,4,5,6]}).set_index('Country_Names')
print(countries)
# Values1 Values2
# Country_Names
# China 1.500 1
# United States 10.530 2
# Japan 7.542 3
# United Kingdom 3.487 4
# Russian Federation 6.565 5
# Brazil 8.189 6
Incidentally, each column of a dataframe is a series, sharing an index with the dataframe to which it belongs. That said, you could have only one column and it would still be a dataframe, though accessing column one would return a series (see below).
Both dataframes and series' have the index attribute in common, as well as other attributes.
countries.index[2] #The 3rd index of the dataframe:
# 'Japan'
countries['Values1'].index[2] #The 3rd index of the 1st column (which is a series)
# 'Japan'
countries.iloc[2] #The 3rd row of the dataframe.
# Values1 7.542
# Values2 3.000
# Name: Japan, dtype: float64
countries['Values1'].iloc[2] #The 3rd row of the 1st column (which is a series)
# 7.542
Now, if you are in fact solely dealing with a series (as your code suggests) and not a dataframe, it would look like this:
Country_Names = ['China', 'United States', 'Japan', 'United Kingdom', 'Russian Federation', 'Brazil']
countries = pd.Series([1.5, 10.53, 7.542, 3.487, 6.565, 8.189], index=Country_Names)
countries
# China 1.500
# United States 10.530
# Japan 7.542
# United Kingdom 3.487
# Russian Federation 6.565
# Brazil 8.189
# dtype: float64
countries.index[2]
# 'Japan'
countries.iloc[2]
# 7.542
I'm not sure how to construct a data object that prints out like the table you have a picture of in your question, though.
Edit
This is how to do it. Create an index with a name and give that index to a series:
Country_Names = pd.Index(['China', 'United States', 'Japan', 'United Kingdom', 'Russian Federation', 'Brazil'],
name='Country_Names')
countries_s = pd.Series([1.5, 10.53, 7.542, 3.487, 6.565, 8.189], index=Country_Names)
countries_s
# Country_Names
# China 1.500
# United States 10.530
# Japan 7.542
# United Kingdom 3.487
# Russian Federation 6.565
# Brazil 8.189
# dtype: float64
That pretty much confirms that you are working with a series. I'm not sure it's possible to have a dataframe with unnamed columns anyway.
Upvotes: 1
Reputation: 4008
call the index directly
return countries.index[2]
but what you post here looks like a pandas dataframe instead of a series - if that's the case do
countries['Country_Name'].iloc[2]
Upvotes: 11