How to Get First Row of Pandas DataFrame? - GeeksforGeeks

30 Nov, 2021
In this article, we will discuss how to get the first row of the pandas dataframe
This method is used to access the row by using row numbers. We can get the first row by using 0 indexes.
Syntax:
dataframe.iloc[0]
where dataframe is the input dataframe
we can also provide the range index.
Syntax:
dataframe.iloc[:1]
Here the rows will be extracted from the start till the index -1 mentioned after the right side of :.
Example: Python code to get the first row of the dataframe by using iloc[] function
Python3
# import pandas moduleimport pandas as pd # create dataframe with 3 columnsdata = pd.DataFrame({    "id": [7058, 7059, 7072, 7054],    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],    "subjects": ['java', 'python', 'html/php', 'php/js']}) # get first row using row positionprint(data.iloc[0]) print("---------------") # get first row using slice operatorprint(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 moduleimport pandas as pd # create dataframe with 3 columnsdata = pd.DataFrame({    "id": [7058, 7059, 7072, 7054],    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],    "subjects": ['java', 'python', 'html/php', 'php/js']}) # get first row using row positionprint(data['name'].iloc[0]) print("---------------") # get first row using slice operatorprint(data['subjects'].iloc[:1])
Output:
sravan
---------------
0    java
Name: subjects, dtype: object
This function will default return the first 5 rows of the dataframe. to get only first row we have to specify 1 
Syntax:
dataframe.head(1)
Example 1: Program to get the first row of the dataset
Python3
# import pandas moduleimport pandas as pd # create dataframe with 3 columnsdata = pd.DataFrame({    "id": [7058, 7059, 7072, 7054],    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],    "subjects": ['java', 'python', 'html/php', 'php/js']}) # get first row using head() functionprint(data.head(1))
Output:
 id    name subjects
0  7058  sravan     java
Example 2: Get the first row for a particular column
Python3
# import pandas moduleimport pandas as pd # create dataframe with 3 columnsdata = pd.DataFrame({    "id": [7058, 7059, 7072, 7054],    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],    "subjects": ['java', 'python', 'html/php', 'php/js']}) # get first row using head() functionprint(data['id'].head(1))
Output:
0    7058
Name: id, dtype: int64
This method is used to get the first row with index function.
Syntax:
dataframe.loc[dataframe.index[0]]
where,
dataframe is the input dataframe
index is the function to get first row
Example: Program to get the first row of the dataset
Python3
# import pandas moduleimport pandas as pd # create dataframe with 3 columnsdata = pd.DataFrame({    "id": [7058, 7059, 7072, 7054],    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],    "subjects": ['java', 'python', 'html/php', 'php/js']}) # get first row using loc() functiondata.loc[data.index[0]]
Output:
id            7058
name        sravan
subjects      java
Name: 0, dtype: object
This will return the first row in the form of an array. Works similar to iloc().
Syntax:
dataframe.values[0]
dataframe.values[:1]
Example: Program to get first row of the dataset
Python3
# import pandas moduleimport pandas as pd # create dataframe with 3 columnsdata = pd.DataFrame({    "id": [7058, 7059, 7072, 7054],    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],    "subjects": ['java', 'python', 'html/php', 'php/js']}) # get first row using loc() functionprint(data.values[:1]) # get first row using loc() functionprint(data.values[:1]) # get particular columnprint(data['name'].values[:1])
Output:
[[7058 'sravan' 'java']]
[[7058 'sravan' 'java']]
['sravan']
This function takes row and column index to display data in the dataframe
Syntax:
dataframe.iat[row_index, column_index]
where
dataframe is the input dataframe
row_index is the row number
column_index is the column number
Example: Program to get the first row of the dataset
Python3
# import pandas moduleimport pandas as pd # create dataframe with 3 columnsdata = pd.DataFrame({    "id": [7058, 7059, 7072, 7054],    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],    "subjects": ['java', 'python', 'html/php', 'php/js']}) # get first row using iat() function of# first row 2 nd columnprint(data.iat[0, 1]) # get first row using iat() function of# first row 1 st columnprint(data.iat[0, 0]) # get first row using iat() function of# first row 3 rd columnprint(data.iat[0, 2])
Output:
sravan
7058
java
This function takes column names along with a first-row index to display the first row of the dataframe.
Syntax:
dataframe.at[row_index, column_name]
where
dataframe is the input dataframe
row_index – 0 to get the first row
column_name is the name of the column
Example: Program to get the first row of the dataset
Python3
# import pandas moduleimport pandas as pd # create dataframe with 3 columnsdata = pd.DataFrame({    "id": [7058, 7059, 7072, 7054],    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],    "subjects": ['java', 'python', 'html/php', 'php/js']}) # get first row using iat() function of# first row 2 nd columnprint(data.at[0, 'name']) # get first row using iat() function of# first row 1 st columnprint(data.at[0, 'id']) # get first row using iat() function of# first row 3 rd columnprint(data.at[0, 'subjects'])
Output:
sravan
7058
java
kapoorsagar226
pandas-dataframe-program
Picked
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Box Plot in Python using Matplotlib
Bar Plot in Matplotlib
Python | Get dictionary keys as a list
Python | Convert set into a list
Ways to filter Pandas DataFrame by column values
Python - Call function from another file
loops in python
Multithreading in Python | Set 2 (Synchronization)
Python Dictionary keys() method
Python Lambda Functions