Pandas DataFrame – Select Column by Index

Pandas DataFrame – Select Column by Index

To select a column by index in DataFrame in Pandas, you can use iloc property of the DataFrame. DataFrame.iloc property lets us choose one or more columns based on index from the DataFrame.

The syntax to select one or more columns by index from a DataFrame df is

df.iloc[:,column_indices_list]

Selecting first column by index from DataFrame

For example, if you would like to select the first column of a DataFrame df, then use the following expression.

df.iloc[:,[0]]

Inside the square brackets, : specifies to select all the rows, and [0] specifies to select the column with index=0.

Selecting third column by index from DataFrame

For example, if you would like to select the third column of a DataFrame df, then use the following expression.

df.iloc[:,[2]]

: specifies to select all the rows, and [2] specifies to select the column with index=2.

The expressions mentioned above return a DataFrame with the selected column of the original DataFrame.

Examples

1. Select first column of DataFrame using DataFrame.iloc property in Pandas

To select the first column of DataFrame in Pandas, you can use DataFrame.iloc property as shown below.

df.iloc[:, [0]]

[0] selects column with index=0.

In the following program, we are given a DataFrame in df_1 with three columns: A, B, and C, and five rows. We have to select the column with index=0 in the given DataFrame df_1 using iloc property.

  1. Given a DataFrame in df_1 with three columns: A, B, and C.
  2. Use iloc property of the DataFrame df_1. Select all the rows, and select the specified column with index=0.
df_1.iloc[:, [0]]
  1. The expression in the above step returns a DataFrame with the first column from the original DataFrame df_1. Store the returned DataFrame in selected_column.
  2. You may print the DataFrame selected_column to output.

Python Program

import pandas as pd

# Take a DataFrame
df_1 = pd.DataFrame({
    'A': [2, 4, 6, 8, 10],
    'B': [1, 3, 5, 7, 9],
    'C': [5, 10, 15, 20, 25]
})

# Select first column from DataFrame
selected_column = df_1.iloc[:, [0]]
print(selected_column)
Run Code Copy

Output

    A
0   2
1   4
2   6
3   8
4  10

2. Select third column from DataFrame using DataFrame.iloc property

To select the third column whose index=2 in the DataFrame, use the following expression.

df.iloc[:, [2]]

In the following program, we have to select the third column from the DataFrame.

  1. Given a DataFrame in df_1 with three columns: A, B, and C.
  2. Use iloc property of the DataFrame df_1. Select all the rows, and select the specified column with index equal to 2.
df_1.iloc[:, [2]]
  1. The expression in the above step returns a DataFrame with all the rows and selected column. Store the returned DataFrame in selected_column.
  2. You may print the DataFrame selected_column to output.

Python Program

import pandas as pd

# Take a DataFrame
df_1 = pd.DataFrame({
    'A': [2, 4, 6, 8, 10],
    'B': [1, 3, 5, 7, 9],
    'C': [5, 10, 15, 20, 25]
})

# Select the third column from DataFrame
selected_column = df_1.iloc[:, [2]]
print(selected_column)
Run Code Copy

Output

    C
0   5
1  10
2  15
3  20
4  25

Summary

In this Pandas Tutorial, we learned how to select a column specified by index from a DataFrame using iloc property of DataFrame instance. The first example covered how to select a column whose index=0 from the DataFrame, and the second example covered how to select a column whose index=2 from the DataFrame.

Related Tutorials

Code copied to clipboard successfully 👍