How to use head() function In Python Pandas

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:



How to Get First Column of Pandas DataFrame?

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

Similar Reads

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....

Method 2 : Using columns[]

...

Method 3: Using column name

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

Method 4: Using head() function

...

Contact Us