Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...