Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 655
D3VLPR Avatar asked Dec 21 '16 22:12

D3VLPR


People also ask

How do you access the index of a Pandas series?

In order to access the series element refers to the index number. Use the index operator [ ] to access an element in a series. The index must be an integer. In order to access multiple elements from a series, we use Slice operation.

How do you get the index of an element in a DataFrame in Pandas?

The get_loc() function is used to find the index of any column in the Python pandas dataframe. We simply pass the column name to get_loc() function to find index.

Can Pandas series have index?

Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.


1 Answers

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() 
like image 173
piRSquared Avatar answered Oct 18 '22 19:10

piRSquared