Menu Close

How to Display First 10 Rows in Pandas DataFrame

How to display first 10 rows in Pandas DataFrame

In this article, we are going to see how to display first 10 rows in Pandas DataFrame with the help of the examples. Pandas Provide a DataFrame method called head() that is used to display the number of rows passed into the head() method. It takes an integer value as a parameter and returns the same number of rows from Pandas DatFrame.

In real-life Pandas applications, sometimes we want to get first some rows to apply some as sample DataFrame to apply some functionality instead of applying the same functionality on entire rows., In that scenario, we can use a head() method that returns a specific number of rows from Pandas DataFrame.

To apply the head() method we must have a Pandas DataFrame.

Let’s create a sample Pandas DataFrame so that we can easily apply the head() method on top of that DataFrame.

Sample Pandas DataFrame

To create a sample Pandas DataFrame I have prepared a simple CSV file along with some records as you can see below.

employees.csv
emp_full_name,emp_email,emp_gender,emp_salary,emp_department,date_of_joining
Mayank Kumar,[email protected],Male,25000,BPO,11/1/2023
Vishvajit Rao,[email protected],Male,40000,IT,11/2/2023
Harshita Mathur,[email protected],Female,20000,Sales,11/3/2023
Kavya Singh,[email protected],Female,20000,SEO,11/4/2023
Vishal Kumar,[email protected],Male,60000,IT,11/5/2023
Vaishali Mehta,[email protected],Female,35000,SEO,11/6/2023
Vaishali Mehta,[email protected],Female,35000,SEO,11/6/2023
James Bond,[email protected],Male,42000,IT,11/7/2023
Mariya Katherine,[email protected],Female,32000,Sales,11/8/2023
Mariya Katherine,[email protected],Female,40000,Sales,11/8/2023
Harshali Kumari,[email protected],Female,21000,BPO,11/9/2023
Vinay Singh,[email protected],Male,18000,BPO,11/10/2023
Vinay Mehra,[email protected],Male,45000,IT,11/11/2023
Akshara Singh,[email protected],Female,55000,IT,11/12/2023

Now, I have loaded the above CSV file using the Pandas read_csv() method.

import pandas as pd
df = pd.read_csv(
                 '../../Datasets/employees.csv'
                )
dfimport pandas as pd
df = pd.read_csv(
                 '../../Datasets/employees.csv'
                )
print(df)
Pandas Sample DataFrame
Pandas Sample DataFrame

Now, we have successfully loaded the CSV file into Pandas DataFrame.

Before displaying the 10 rows in Pandas DataFrame let’s see a little about the DataFrame head() method.

Pandas DataFrame head() Method

The head() is the Pandas DataFrame method that is always applied on the top of the Pandas DataFrame and it takes an integer value as a parameter that indicates the number of rows.

The head() method returns a new data frame along with a specified number of rows.

Let’s see the head() method practically.

Display First 10 Rows in Pandas DataFrame

Here we are about to display only 10 rows from the above Pandas DataFrame method. To display the first 10 rows, we need to call the head() method and pass 10 as a parameter into the head() method.

Example: Display First 10 Rows in Pandas DataFrame

import pandas as pd
df = pd.read_csv(
                 '../../Datasets/employees.csv'
                )
new_df = df.head(10)
print(new_df)
Display First 10 Rows in Pandas DataFrame
Display First 10 Rows in Pandas DataFrame

So this is how you can display the first 10 rows in Pandas DataFrame with the help of the head() method.


Helpful Pandas Articles


Conclusion

So throughout this article, we have seen how to display the first 10 rows in Pandas DataFrame method. Using this head() method you can display first any number of rows easily and perform some operations on top of the newly returned DataFrame.

if you found this article helpful, please share and keep visiting for further Pandas tutorials.

How to Explode Multiple Columns in Pandas
How to Get Top 10 Highest Values in Pandas DataFrame

Related Posts