How to Access Rows by Numeric Index in Pandas (Python)

How to Access Rows by Numeric Index in Pandas (Python)

Channel:
Subscribers:
53,100
Published on ● Video Link: https://www.youtube.com/watch?v=lLhcFkPwaqs



Category:
Guide
Duration: 2:34
2,738 views
59


↓ Code Available Below! ↓

This video shows how to access the rows of a pandas data frame using the numeric index of the row using iloc. Using numeric indexes to get rows can be more natural than using the row index names because base Python data structures like lists don't have index names.

If you find this video useful, like, share and subscribe to support the channel!
► Subscribe: https://www.youtube.com/c/DataDaft?sub_confirmation=1


Code used in this Python Code Clip:

import pandas as pd

import statsmodels.api as sm #(To access mtcars dataset)
mtcars = sm.datasets.get_rdataset("mtcars", "datasets", cache=True).data
mtcars.head()

# Access a row by numerical index with .iloc
mtcars.iloc[2]

# Access multiple rows by numerical index
mtcars.iloc[[2,4,5]]

# Access multiple rows and specific columns (by numeric index)
mtcars.iloc[[2,4,5], [0, 1, 3]]

# Access multiple rows and specific columns (by col name)
mtcars.iloc[[2,4,5]][["mpg", "cyl"]]


* Note: YouTube does not allow greater than or less than symbols in the text description, so the code above will not be exactly the same as the code shown in the video! I will use Unicode large < and > symbols in place of the standard sized ones. .


⭐ Kite is a free AI-powered coding assistant that integrates with popular editors and IDEs to give you smart code completions and docs while you’re typing. It is a cool application of machine learning that can also help you code faster! Check it out here: https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=datadaft&utm_content=description-only







Tags:
get rows by numeric index
pandas row indexing
pandas iloc
.iloc[]
iloc
pandas indexing
pandas numeric index
pandas use numeric index
pandas access rows by number
pandas numeric indexing
pandas integer indexing
data frame numeric indexing
data frame indexing
pandas subset rows
pandas get specific rows
pandas row filtering
pandas basics
pandas filtering
pandas select rows
pandas select rows by numeric index
pandas subset rows by numeric index
python basics