Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Get First Column of Pandas DataFrame?

  • Last Updated : 28 Nov, 2021

In this article, we will discuss how to get the first column of the pandas dataframe in Python programming language.

Method 1: Using iloc[] function

This function is used to get the first column using slice operator. for the rows we extract all of them, for columns specify the index for first column.

Syntax :

dataframe.iloc[:, 0]

where. dataframe is the input dataframe

The slice operation will be like:

[row_start:row_end , column_start, column_end]

where,

  • row_start refers start row with 0 position as index
  • row_end refers last row with n th  position as index
  • column_start refers start column with 0 position as index
  • column_end refers last column with n th position as index

The following alternative can also be employed.

Syntax :

dataframe.iloc[:, :1]

where. dataframe is the input dataframe. Both of these will return the dataframe datatype.

Example: Python program to get the first column by using above approaches

Python3




# import pandas module
import pandas as pd
  
# create dataframe with 3 columns
data = pd.DataFrame({
    "id": [7058, 7059, 7072, 7054],
    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],
    "subjects": ['java', 'python', 'html/php', 'php/js']
}
)
  
# display dataframe
print(data)
  
print("--------------")
  
# get first column by returning series
print(data.iloc[:, 0])
  
print("--------------")
  
# get first column by returning dataframe
print(data.iloc[:, :1])

Output:

Method 2 : Using columns[]

This method will return the column based on index. So, we have to give 0 to get the first column

Syntax:

dataframe[dataframe.columns[0]]

where

  • dataframe is the input dataframe
  • columns[0] represent first column

Example: Python program to get the first column using columns[]

Python3




# import pandas module
import pandas as pd
  
# create dataframe with 3 columns
data = pd.DataFrame({
    "id": [7058, 7059, 7072, 7054],
    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],
    "subjects": ['java', 'python', 'html/php', 'php/js']
}
)
  
# display dataframe
print(data)
  
print("--------------")
  
  
# get first column by returning dataframe
# using columns[] method
print(data[data.columns[0]])

Output:

Method 3: Using column name

We can use the first column name to get the first column.

Syntax:

dataframe.first_column

Example: Python code to get the first column using column name

Python3




# import pandas module
import pandas as pd
  
# create dataframe with 3 columns
data = pd.DataFrame({
    "id": [7058, 7059, 7072, 7054],
    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],
    "subjects": ['java', 'python', 'html/php', 'php/js']
}
)
  
# display dataframe
print(data)
  
print("--------------")
  
  
# get first column by returning dataframe
# using column_name
print(data.id)

Output:

We can also use the head() function within this to display the number of rows in the first column.

Example: Python code to get the first column using the column name

Python3




# import pandas module
import pandas as pd
  
# create dataframe with 3 columns
data = pd.DataFrame({
    "id": [7058, 7059, 7072, 7054],
    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],
    "subjects": ['java', 'python', 'html/php', 'php/js']
}
)
  
# display dataframe
print(data)
  
print("--------------")
  
  
# get first column by returning dataframe
# using column_name
# display 1 row
print(data.id.head(1))
print("--------------")
  
# get first column by returning dataframe
# using column_name
# display 2 rows
print(data.id.head(2))
print("--------------")
  
# get first column by returning dataframe
# using column_name
# display 4 rows
print(data.id.head(4))

Output:

Method 4: Using head() function

This function by default returns the top rows of the dataframe. To return the column we have to Transpose (Interchange rows to columns) the dataframe by using T function and get 1st column.

Syntax:

dataframe.T.head(1).T

Example: Python code to get the first column using column name

Python3




# import pandas module
import pandas as pd
  
# create dataframe with 3 columns
data = pd.DataFrame({
    "id": [7058, 7059, 7072, 7054],
    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],
    "subjects": ['java', 'python', 'html/php', 'php/js']
}
)
  
# display dataframe
print(data)
  
print("--------------")
# using head
print(data.T.head(1).T)

Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!