. Advertisement .
..3..
. Advertisement .
..4..
A Pandas dataframe is a great source to store values as columns and rows. The following article gives detailed explanations for some methods to get the first data frame row in Pandas.
How To Get The First DataFrame Row In Pandas
Method 1: Use The pandas.dataframe.iloc Property
Using the pandas.dataframe.iloc
property is one of the most commonly used approaches to get the first dataframe row in Pandas. It allows users to access a row with row numbers. You can use 0 indexes to get the first one.
The iloc property is an ideal tool for selecting Integer-based rows. Thus, you need to pass the column and row indexes to pick the cell values of a dataframe.
As the first index is 0, you need to employ 0 in the appropriate row and column index in the attribute.
Syntax:
dataframe.iloc[0]
Code:
import pandas as pd
df = pd.DataFrame({
'list1': ["ONE","TWO","THREE","FOUR"],
'list2': [25,28,18,11],
'list3': [786, 656, 271, 933],
'list4': ['Fire', 'Pandas', 'Numpy', 'Matplotlib']
})
row_1st=df.iloc[0]
print("The DataFrame is:")
print(df,"\n")
print("The first row of the DataFrame is:")
print(row_1st)
Output:
The DataFrame is:
list1 list2 list3 list4
0 ONE 25 786 Fire
1 TWO 28 656 Pandas
2 THREE 18 271 Numpy
3 FOUR 11 933 Matplotlib
The first row of the DataFrame is:
list1 ONE
list2 25
list3 786
list4 Fire
Name: 0, dtype: object
Method 2: Use The pandas.dataframe.head() Method
This function can be employed to return a dataframe with the first five rows. By using this option, you can pass a number as an argument, which represents the the number of selected rows.
It prints the dataframe’s first n rows and checks its correct type of data. If you want to print some rows of a specific column, that column’s subset is selected with the df[[ column name]]
.
For example, number 1 can be passed as an argument to the function to select the dataframe’s first row.
Code:
import pandas as pd
df = pd.DataFrame({
'list1': ["ONE","TWO","THREE","FOUR"],
'list2': [25,28,18,11],
'list3': [786, 656, 271, 933],
'list4': ['Fire', 'Pandas', 'Numpy', 'Matplotlib']
})
row_1st=df.head(1)
print("The DataFrame is:")
print(df,"\n")
print("The first row of the DataFrame is:")
print(row_1st)
Output:
The DataFrame is:
list1 list2 list3 list4
0 ONE 25 786 Fire
1 TWO 28 656 Pandas
2 THREE 18 271 Numpy
3 FOUR 11 933 Matplotlib
The first row of the DataFrame is:
list1 list2 list3 list4
0 ONE 25 786 Fire
Method 3: Get From A Specified Condition
You can get the first row, which satisfies some specified coniditons from a dataframe. At first, filter the rows and select the one from the filtered dataframe. Combine all methods mentioned above to accomplish the task.
Code:
import pandas as pd
df = pd.DataFrame({
'list1': ["ONE","TWO","THREE","FOUR"],
'list2': [25,28,18,11],
'list3': [786, 656, 271, 933],
'list4': ['Fire', 'Pandas', 'Numpy', 'Matplotlib']
})
DFfilter=df[(df.list2 > 17) & (df.list3 < 700)]
row_1st_DFfilter=DFfilter.head(1)
print("The DataFrame is:")
print(df,"\n")
print("The DFfilter is:")
print(DFfilter,"\n")
print("The first row with list2 greater than 17 and list3 less than 700 is:")
print(row_1st_DFfilter)
Output:
The DataFrame is:
list1 list2 list3 list4
0 ONE 25 786 Fire
1 TWO 28 656 Pandas
2 THREE 18 271 Numpy
3 FOUR 11 933 Matplotlib
The DFfilter is:
list1 list2 list3 list4
1 TWO 28 656 Pandas
2 THREE 18 271 Numpy
The first row with list2 greater than 17 and list3 less than 700 is:
list1 list2 list3 list4
1 TWO 28 656 Pandas
The output of this example represents the first row with the column’s value greater than 17 and less than 700. In such cases, the query()
function will also work out.
Method 4: Use The iat[] Function
The iat[] function is not always a commonly used method. It takes column and row indexes to represent data in the dataframe. Here is the syntax:
dataframe.iat[row_index, column_index]
In this one, the dataframe will give the input one while row_index
and column_index
gives the row and column numbers.
Code:
import pandas as pd
df = pd.DataFrame({
'list1': ["ONE","TWO","THREE","FOUR"],
'list2': [25,28,18,11],
'list3': [786, 656, 271, 933],
'list4': ['Fire', 'Pandas', 'Numpy', 'Matplotlib']
})
print("The DataFrame is:")
print(df,"\n")
print("The first row of the DataFrame is:")
print(df.iat[0,0], df.iat[0,1],df.iat[0,2], df.iat[0,3])
Output:
The DataFrame is:
list1 list2 list3 list4
0 ONE 25 786 Fire
1 TWO 28 656 Pandas
2 THREE 18 271 Numpy
3 FOUR 11 933 Matplotlib
The first row of the DataFrame is:
ONE 25 786 Fire
Conclusion
Overall, the article has guided you on the best methods to get the first dataframe row in Pandas. Choose the one that suits your data and cases.
Leave a comment