When working with massive datasets with incorrect data and you might get errors while preprocessing it. You may need to see the value of a specific row to identify the root cause of the errors.
You can use the df.loc[[2]] to print a specific row of a pandas dataframe.
In this tutorial, you’ll learn the different methods to print a specific row of a pandas dataframe.
If you’re in Hurry
You can use the loc
property to select and print a specific row of pandas dataframe.
df.loc[[1]]
The second row of the dataframe will be printed.
Output
sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | target | |
---|---|---|---|---|---|
2 | 4.7 | 3.2 | 1.3 | 0.2 | 0 |
If You Want to Understand Details, Read on…
When manipulating erroneous rows of the massive datasets, you may get some errors. In that case, you may need to print the particular row of the dataframe to identify the reason for the errors.
There are different methods to print specific rows of a dataframe. Let us discuss those methods in detail.
Table of Contents
Sample Dataframe
First, let us create a sample dataframe. The sample dataframe is directly loaded from the sklearn library and converted into a pandas dataframe, as demonstrated below.
import pandas as pd
from sklearn import datasets
iris = datasets.load_iris()
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
df["target"] = iris.target
df.head()
Dataframe will look like
sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | target | |
---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | 0 |
1 | 4.9 | 3.0 | 1.4 | 0.2 | 0 |
2 | 4.7 | 3.2 | 1.3 | 0.2 | 0 |
3 | 4.6 | 3.1 | 1.5 | 0.2 | 0 |
4 | 5.0 | 3.6 | 1.4 | 0.2 | 0 |
Now, you’ll print the specific row of the pandas dataframe using different methods.
You can also select rows from the pandas dataframe based on conditions to visualise the data.
Using LOC
In this section, you’ll use the pandas dataframe’s loc
property to select rows by index and print it.
Loc property uses the label to select rows and columns.
The pandas dataframe rows will have indexes, which are the labels of the row axis. The index will be a number starting from 0
.
loc
selects the rows using its label.
If you pass only one scalar value to the loc
property, you’ll see a specific row returned as a pandas series.
df.loc[1]
Output
sepal length (cm) 6.3
sepal width (cm) 3.3
petal length (cm) 6.0
petal width (cm) 2.5
target 2.0
Name: 100, dtype: float64
To select a row similar to the dataframe row, you can pass the row numbers as a range.
For example, you can use the below statement to select the second row of the dataframe.
df.loc[1:1]
Output
sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | target | |
---|---|---|---|---|---|
2 | 4.7 | 3.2 | 1.3 | 0.2 | 0 |
You can also pass the row number as a list below.
This prints the second row of the pandas dataframe
df.loc[[1]]
You can pass the row numbers as a list to print more than one row.
df.loc[[1,2]]
This will print the second and third rows of the dataframe.
Using iLOC
In this section, you’ll use the iLOC
property of the dataframe to print a specific row of the dataframe.
iLOC property uses the index number to select the rows from the pandas dataframe. It is a primarily integer-based selector.
It also accepts an integer and returns the row as a pandas series.
df.iloc[100]
Output
sepal length (cm) 6.3
sepal width (cm) 3.3
petal length (cm) 6.0
petal width (cm) 2.5
target 2.0
Name: 100, dtype: float64
To print the row similar to the dataframe row, you can pass the row number as a list.
df.iloc[[100]]
Dataframe will look like
sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | target | |
---|---|---|---|---|---|
2 | 4.7 | 3.2 | 1.3 | 0.2 | 0 |
Printing Specific Row and Column
You can use LOC
and iLOC
properties to print a specific row and column from the pandas dataframe.
Using LOC
To print a specific cell value in the pandas dataframe, use the statement below. It prints the value from the first row and the column sepal length (cm).
There are two parameters.
- Row label
- Column label
df.loc[0,'sepal length (cm)']
Output
5.1
Using iLOC
To print the first two columns from the first row, use the below snippet.
df.iloc[[0],0:2]
Output
sepal length (cm) | sepal width (cm) | |
---|---|---|
0 | 5.1 | 3.5 |
Conclusion
To summarise, you’ve learned how to print a specific row of a pandas dataframe. This will be useful to visualise the data which seems to be invalid.
If you have any questions, please comment below.