Skip to content
Advertisement

How to get the index of ith item in pandas.Series or pandas.DataFrame?

I’m trying to get the index of 6th item in a Series I have.

This is how the head looks like:

United States    1.536434e+13
China            6.348609e+12
Japan            5.542208e+12
Germany          3.493025e+12
France           2.681725e+12

For getting the 6th index name (6th Country after being sorted), I usually use s.head(6) and get the 6th index from there.

s.head(6) gives me:

United States     1.536434e+13
China             6.348609e+12
Japan             5.542208e+12
Germany           3.493025e+12
France            2.681725e+12
United Kingdom    2.487907e+12

and looking at this, I’m getting the index as United Kingdom.

So, is there any better way for getting the index other than this? And also, for a dataframe, is there any function to get the 6th index on basis of a respective column after sorting.

If it’s a dataframe, I usually, sort, create a new column named index, and use reset_index, and then use iloc attribute to get the 6th (since it will be using a range in the index after reset).

Is there any better way to do this with pd.Series and pd.DataFrame?

Advertisement

Answer

You could get it straight from the index

s.index[5]

Or

s.index.values[5]

It all depends on what you consider better. I can tell you that a numpy approach will probably be faster.

For example. numpy.argsort returns an array where the first element in the array is the position in the array being sorted that should be first. The second element in argsort’s return array is the position of the element in the array being sorted that should be second. So on and so forth.

So you could do this to get the index value of the 6th item after being sorted.

s.index.values[s.values.argsort()[5]]

Or more transparently

s.sort_values().index[5]

Or more creatively

s.nsmallest(6).idxmax()
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement