Pandas Accessing Columns by index

| 0 Comments| 11:28 am


Pandas: How to Access or Select Columns by Index, not by Name

In Pandas, accessing columns by their index is useful when you want to retrieve specific columns based on their position, rather than their name. This can be done using various methods, such as iloc[], iat[], or using columns to get the column name by position. In this article, we’ll explore these methods with examples.

Method 1: Access Column by Index Using iloc[]

iloc[] is used to access columns by their integer position (index). This method is very useful when you need to access columns in a specific order, regardless of the column names.

Example: Access Column by Index Using iloc[]

import pandas as pd

# Sample DataFrame
data = {'Name': ['John', 'Alice', 'Bob'],
        'Age': [25, 30, 22],
        'Gender': ['Male', 'Female', 'Male']}
df = pd.DataFrame(data)

# Access the column at index position 1 (second column, 'Age')
column = df.iloc[:, 1]
print(column)

Output:

0    25
1    30
2    22
Name: Age, dtype: int64

In this example, we use iloc[] to access the column at position 1 (the second column). The result is the ‘Age’ column.

Method 2: Access Column by Index Using iat[]

iat[] is similar to iloc[], but it is used for accessing a single element at a specific row and column index. It’s very efficient for accessing individual values in the DataFrame.

Example: Access Column by Index Using iat[]

# Access the first element in the second column (Age)
value = df.iat[0, 1]
print(value)

Output:

25

In this example, we use iat[] to access the value at row 0, column 1 (the first element of the ‘Age’ column).

Method 3: Access Column by Index Using columns

Another way to access a column by its index is by using the columns attribute, which stores the column labels. You can use the column index to get the column name and then access the column using loc[] or iloc[].

Example: Access Column by Index Using columns

# Get the column name at index position 1
column_name = df.columns[1]

# Access the column using loc[]
column = df[column_name]
print(column)

Output:

0    25
1    30
2    22
Name: Age, dtype: int64

In this example, we first use df.columns[1] to retrieve the column name at position 1 (which is ‘Age’), and then we access that column using df[column_name].

Method 4: Access Multiple Columns by Index Using iloc[]

You can also use iloc[] to select multiple columns by their index positions.

Example: Access Multiple Columns by Index Using iloc[]

# Access columns at index positions 0 and 2
columns = df.iloc[:, [0, 2]]
print(columns)

Output:

    Name  Gender
0   John    Male
1  Alice  Female
2    Bob    Male

In this example, we use iloc[] to select multiple columns at positions 0 and 2 (‘Name’ and ‘Gender’).

Summary

Accessing columns by index in Pandas can be done using iloc[], iat[], or by using the columns attribute to retrieve the column names. iloc[] is useful for accessing columns by integer positions, while iat[] is used for retrieving specific values from a column at a specific index. By combining these methods, you can efficiently access and manipulate columns in a DataFrame based on their positions.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended Post