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.
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.
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.
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.
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).
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[]
.
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]
.
iloc[]
You can also use iloc[]
to select multiple columns by their index positions.
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’).
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.
Pandas: How to Access Columns by Name In Pandas, accessing columns by name is a…
Pandas: How to Access Row by Index In Pandas, you can access rows in a…
Pandas: How to Access a Column Using iterrows() In Pandas, iterrows() is commonly used to…
Pandas - How to Update Values in iterrows In Pandas, iterrows() is a popular method…
Pandas KeyError When Using iterrows() In Pandas, the iterrows() method is often used to iterate…
Pandas DataFrame KeyError: 0 - Trying to access column or index that does not exist…