Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query Pandas Dataframe Index

Tags:

python

pandas

I have a simple index on my Dataframe (integers from 0:n).

If I want row index values 1,10,100, how do I query the index to get only those rows back?

Thanks

like image 582
code base 5000 Avatar asked Jun 13 '26 11:06

code base 5000


2 Answers

In your case, the index equals to integer position. Thus you can use either .loc or .iloc.

.loc is primarily label based...

.iloc is primarily integer position based (from 0 to length-1 of the axis)

See different-choices-for-indexing

like image 93
gzc Avatar answered Jun 16 '26 06:06

gzc


Try this:

result = data.loc[[1,10,100]]
like image 36
Calvin Smythe Avatar answered Jun 16 '26 08:06

Calvin Smythe