How to Get First Row of Pandas DataFrame?
In this article, we will discuss how to get the first row of the Pandas Dataframe
Get the First Row of Pandas using iloc[]
This method is used to access the row by using row numbers. We can get the first row by using 0 indexes.
Example 1: Python code to get the first row of the Dataframe by using the iloc[] function
Python3
import pandas as pd
data = pd.DataFrame({
"id" : [ 7058 , 7059 , 7072 , 7054 ],
"name" : [ 'sravan' , 'jyothika' , 'harsha' , 'ramya' ],
"subjects" : [ 'java' , 'python' , 'html/php' , 'php/js' ]
}
)
print (data.iloc[ 0 ])
print ( "---------------" )
print (data.iloc[: 1 ])
|
Output:
id 7058
name sravan
subjects java
Name: 0, dtype: object
---------------
id name subjects
0 7058 sravan java
Example 2: Get the first row for a particular column
Python3
import pandas as pd
print (data[ 'name' ].iloc[ 0 ])
print ( "---------------" )
print (data[ 'subjects' ].iloc[: 1 ])
|
Output:
sravan
---------------
0 java
Name: subjects, dtype: object
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 as pd
data = pd.DataFrame({
"id" : [ 7058 , 7059 , 7072 , 7054 ],
"name" : [ 'sravan' , 'jyothika' , 'harsha' , 'ramya' ],
"subjects" : [ 'java' , 'python' , 'html/php' , 'php/js' ]
}
)
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 as pd
print (data[ 'id' ].head( 1 ))
|
Output:
0 7058
Name: id, dtype: int64
Get the First Row of Pandas using loc()
This method is used to get the first row with the index function. Here, we will see the program to get the first row of the dataset
Python3
import pandas as pd
data.loc[data.index[ 0 ]]
|
Output:
id 7058
name sravan
subjects java
Name: 0, dtype: object
Get the First Row of Pandas using values()
This will return the first row in the form of an array. Works similar to iloc(). Here, we will see the program to get the first row of the dataset.
Python3
import pandas as pd
print (data.values[: 1 ])
print (data.values[: 1 ])
print (data[ 'name' ].values[: 1 ])
|
Output:
[[7058 'sravan' 'java']]
[[7058 'sravan' 'java']]
['sravan']
Get the First Row of Pandas using iat[]
This function takes a row and column indexes to display data in the Dataframe. Here, we will see the program to get the first row of the dataset.
Python3
import pandas as pd
print (data.iat[ 0 , 1 ])
print (data.iat[ 0 , 0 ])
print (data.iat[ 0 , 2 ])
|
Output:
sravan
7058
java
Get the First Row of Pandas using at[]
This function takes column names along with a first-row index to display the first row of the Dataframe. Here, we will see the program to get the first row of the dataset.
Python3
import pandas as pd
print (data.at[ 0 , 'name' ])
print (data.at[ 0 , 'id' ])
print (data.at[ 0 , 'subjects' ])
|
Output:
sravan
7058
java
Last Updated :
16 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...