Pandas: Select first column of dataframe in python

In this article, we will discuss different ways to get or select the first column of dataframe as a series or list object.

Table of Contents

There are different ways to select the first column of this dataframe. Let’s discuss them one by one,

Use iloc[] to select first column of pandas dataframe

In Pandas, the Dataframe provides an attribute iloc[], to select a portion of the dataframe using position based indexing. This selected portion can be few columns or rows . We can use this attribute to select only first column of the dataframe. For example,

# Select first column of the dataframe as a series
first_column = df.iloc[:, 0]

We selected a portion of dataframe as a series object, that included all rows, but only first column of the dataframe.

How did it work?

The syntax of dataframe.iloc[] is like,

df.iloc[row_start:row_end , col_start, col_end]

Arguments:

  • row_start: The row index/position from where it should start selection. Default is 0.
  • row_end: The row index/position from where it should end the selection i.e. select till row_end-1. Default is till the last row of the dataframe.
  • col_start: The column index/position from where it should start selection. Default is 0.
  • col_end: The column index/position from where it should end the selection i.e. select till end-1. Default is till the last column of the dataframe.

It returns a portion of the dataframe that includes rows from row_start to row_end-1 and columns from col_start to col_end-1.

To select the first column of dataframe select from column index 0 till 1 i.e (:1) and select all rows using default values (:),

# Select first column of the dataframe as a dataframe
first_column = df.iloc[: , :1]

We provided the range to select the columns from 0 position till 1 to select the first column, therefore it returned a dataframe. If you want to select the first column as a series object then just pass the 0 instead of range. For example,

# Select first column of the dataframe as a series
first_column = df.iloc[:, 0]

Checkout complete example to select first column of dataframe using iloc,

import pandas as pd

# List of Tuples
empoyees = [('Jack',    34, 'Sydney',   5) ,
            ('Riti',    31, 'Delhi' ,   7) ,
            ('Aadi',    16, 'London',   11) ,
            ('Mark',    41, 'Delhi' ,   12)]

# Create a DataFrame object
df = pd.DataFrame(  empoyees, 
                    columns=['Name', 'Age', 'City', 'Experience'])

print("Contents of the Dataframe : ")
print(df)

# Select first column of the dataframe as a dataframe object
first_column = df.iloc[: , :1]

print("First Column Of Dataframe: ")

print(first_column)
print("Type: " , type(first_column))


# Select first column of the dataframe as a series
first_column = df.iloc[:, 0]

print("First Column Of Dataframe: ")
print(first_column)

print("Type: " , type(first_column))

Output:

Contents of the Dataframe :
   Name  Age    City  Experience
0  Jack   34  Sydney           5
1  Riti   31   Delhi           7
2  Aadi   16  London          11
3  Mark   41   Delhi          12
First Column Of Dataframe:
   Name
0  Jack
1  Riti
2  Aadi
3  Mark
Type:  <class 'pandas.core.frame.DataFrame'>
First Column Of Dataframe:
0    Jack
1    Riti
2    Aadi
3    Mark
Name: Name, dtype: object
Type:  <class 'pandas.core.series.Series'>

We selected the first column of dataframe.

Select first column of pandas dataframe using []

We can fetch the column names of dataframe as a sequence and then select the first column name. Then using that column name, we can select the first column of dataframe as a series object using subscript operator i.e. []. For example,

# Select first column of the dataframe
first_column = df[df.columns[0]]

print("First Column Of Dataframe: ")
print(first_column)

print("Type: " , type(first_column))

Output:

First Column Of Dataframe:
0    Jack
1    Riti
2    Aadi
3    Mark
Name: Name, dtype: object
Type:  <class 'pandas.core.series.Series'>

Use head() to select the first column of pandas dataframe

We can use the dataframe.T attribute to get a transposed view of the dataframe and then call the head(1) function on that view to select the first row i.e. the first column of original dataframe. Then transpose back that series object to have the column contents as a dataframe object. For example,

# Select first column of the dataframe 
first_column = df.T.head(1).T

print("First Column Of Dataframe: ")
print(first_column)

print("Type: " , type(first_column)) 

Output:

First Column Of Dataframe:
   Name
0  Jack
1  Riti
2  Aadi
3  Mark
Type:  <class 'pandas.core.frame.DataFrame'>

It returned the first column of dataframe as a dataframe object.

Pandas: Get first column of dataframe as list

Select the first column of dataframe as a series object using iloc[:, 0] and then call the tolist() function on the series object. It will return the first column of dataframe as a list object. For example,

# Select first Column 
first_column = df.iloc[:, 0].tolist()

print("First Column Of Dataframe: ")
print(first_column)

print("Type: " , type(first_column))

Output:

First Column Of Dataframe:
['Jack', 'Riti', 'Aadi', 'Mark']
Type:  <class 'list'>

It returned the first column of dataframe as a list.

Summary

We learned different ways to get the first column of a dataframe as a series or list object in python.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top