get the nth column of a pandas dataframe

Pandas – Get the Nth column of a Dataframe

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

get the nth column of a pandas dataframe

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:

pandas dataframe with employee information

Example 1

Let’s access the 2nd column, “Age”, using its column index, 1.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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 –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

Scroll to Top