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

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

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



Category:
Guide
Duration: 2:53
3,695 views
65


↓ Code Available Below! ↓

This video shows how to access the columns of a pandas data frame using the numeric index using iloc. Using numeric indexes to get columns can make it easier to select multiple columns or large ranges of columns since you don't have to know or type out all of the column 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 column by numerical index
mtcars.iloc[:, 2]

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

# Access multiple columns and a subset of rows
mtcars.iloc[[1,5,7], [2,4,5]]

# Access columns with a column subset
mtcars[mtcars.columns[[2,4,5]]]


* 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:
pandas select columns by numeric index
pandas column subset
pandas get column by number
pandas .iloc
.iloc
iloc
pandas indexing
pandas subsetting
pandas data frame basics
pandas basics
data frame subsetting
data frame get column by number
pandas access columns by numeric index
pandas data subsetting
learn pandas
basic data manipulation
indexing in pandas
use numeric index in pandas
pandas
python