In this tutorial, we will know how to get the Nth column of a Pandas Dataframe. Later, we will understand the same with the help of a few examples.
How to get the Nth column of a Dataframe using Pandas?
We can get the Nth column of a Pandas Dataframe with the help of the .iloc[]
property using the index of the column to access it.
Syntax: dataFrameName.iloc[:, n-1]
Note : In pandas dataframes, column index starts from 0, so, to access the nth column, use n-1 as the column index in
iloc
. For example, if you want to access the 2nd column then you will 1 as the index of the column to access.
Examples
We will now look at a few examples for a better understanding.
But before that, we will create a pandas dataframe that we will be using throughout this tutorial using the following command:
import pandas as pd # employee data data = { "Name": ["Jim", "Dwight", "Angela", "Tobi"], "Age": [26, 28, 27, 32], "Department": ["Sales", "Sales", "Accounting", "HR"] } # create pandas dataframe df = pd.DataFrame(data) # displays dataframe df
Output:
Example 1
Let’s access the 2nd column, “Age”, using its column index, 1.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# get the 2nd column df.iloc[:, 1]
Output:
0 26 1 28 2 27 3 32 Name: Age_Emp, dtype: int64
Example 2
Let’s now access the 3rd column, “Department”. For this, we will use column index 2.
# get the 3rd column df.iloc[:, 2]
Output:
0 Sales 1 Sales 2 Accounting 3 HR Name: Department, dtype: object
Example 3
You might wonder what will happen if we try to access a column that does not exist in our dataframe. Let’s take an example.
# get the 6th column df.iloc[:, 5]
Output:
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Input In [3], in <module> ----> 1 df.iloc[:, 5] ... IndexError: single positional indexer is out-of-bounds
You can see that, when we tried accessing the column which was not present in the dataframe through its index, we got an
IndexError
.
Summary
In this tutorial, we looked at how to access the Nth column in a Pandas dataframe. The following are the key takeaways –
- Use the
.iloc[]
property to access the Nth column by its index, N-1. - In case the column we want to access is not present in our dataframe, an
IndexError
will be raised.
You might also be interested in –
- Check if a Column Exists in a Pandas DataFrame
- Check if Pandas DataFrame column has object dtype
- Most frequent value in a Pandas Column
- Split Pandas column of lists into multiple columns
- Pandas – Set Column as Index (With Examples)
- Pandas – Check if a column is all one value
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.