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