Get the First Row of Pandas using head()
This function will default return the first 5 rows of the Dataframe. to get only the first row we have to specify 1
Example 1: Program to get the first row of the dataset.
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' ] } ) # get first row using head() function print (data.head( 1 )) |
Output:
id name subjects 0 7058 sravan java
Example 2: Get the first row for a particular column
Python3
# import pandas module import pandas as pd # get first row using head() function print (data[ 'id' ].head( 1 )) |
Output:
0 7058 Name: id, dtype: int64
How to Get First Row of Pandas DataFrame?
In this article, we will discuss how to get the first row of the Pandas Dataframe