In the pandas Python library, we can use the DataFrame.head() method to see the first few rows of a dataset. For example, if we want to see the first 10 rows of a dataset, we can use the DataFrame.head(10) method.
On the other hand, the DataFrame.tail() function gives us the last few rows of a dataset. So, if we want to see the last 10 rows of a dataset, we need to call the function DataFrame.tail(10).
import pandas
df = pandas.read_csv("iris.csv")
print("The first 10 rows: \n", df.head(10))
print("The last 10 rows: \n", df.tail(10))
In the above example, we are first reading the CSV file “iris.csv” and creating a DataFrame df. After that, we are calling the df.head() and df.tail() functions to see the first and last few rows of the dataset, respectively.
The output of the above program will be:
The first 10 rows:
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
5 5.4 3.9 1.7 0.4 setosa
6 4.6 3.4 1.4 0.3 setosa
7 5.0 3.4 1.5 0.2 setosa
8 4.4 2.9 1.4 0.2 setosa
9 4.9 3.1 1.5 0.1 setosa
The last 10 rows:
sepal_length sepal_width petal_length petal_width species
140 6.7 3.1 5.6 2.4 virginica
141 6.9 3.1 5.1 2.3 virginica
142 5.8 2.7 5.1 1.9 virginica
143 6.8 3.2 5.9 2.3 virginica
144 6.7 3.3 5.7 2.5 virginica
145 6.7 3.0 5.2 2.3 virginica
146 6.3 2.5 5.0 1.9 virginica
147 6.5 3.0 5.2 2.0 virginica
148 6.2 3.4 5.4 2.3 virginica
149 5.9 3.0 5.1 1.8 virginica








































0 Comments