Table of Contents
The head() function in Pandas is a useful tool for viewing the first few rows of a dataset. It allows users to quickly get a glimpse of the data and its structure without having to view the entire dataset. This function can be utilized by calling it on a Pandas DataFrame or Series object and specifying the number of rows to be displayed. For example, head(10) will display the first 10 rows of the dataset.
The head() function can be particularly helpful in data exploration and data cleaning tasks. It can help users identify any issues with the data, such as missing values or incorrect data types, in the initial rows of the dataset. It can also be used to check if the data has been imported correctly and to get a general understanding of the data before diving into more in-depth analysis.
Some examples of the usage of the head() function include checking the column names and data types of a dataset, identifying any outliers or anomalies in the initial rows, and verifying if the data has been sorted correctly. Additionally, it can be used to quickly preview the data before applying any manipulations or transformations.
Overall, the head() function is a useful tool for getting a quick overview of a dataset and can save users time and effort in data analysis tasks.
Use Pandas head() Function (With Examples)
You can use the head() function to view the first n rows of a pandas DataFrame.
This function uses the following basic syntax:
df.head()
The following examples show how to use this syntax in practice with the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29], 'assists': [5, 7, 7, 9, 12, 9, 9, 4], 'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]}) #view DataFrame df points assists rebounds 0 25 5 11 1 12 7 8 2 15 7 10 3 14 9 6 4 19 12 6 5 23 9 5 6 25 9 9 7 29 4 12
Example 1: View First 5 Rows of DataFrame
By default, the head() function displays the first five rows of a DataFrame:
#view first five rows of DataFrame
df.head()
points assists rebounds
0 25 5 11
1 12 7 8
2 15 7 10
3 14 9 6
4 19 12 6Example 2: View First n Rows of DataFrame
We can use the n argument to view the first n rows of a pandas DataFrame:
#view first three rows of DataFrame
df.head(n=3)
points assists rebounds
0 25 5 11
1 12 7 8
2 15 7 10Example 3: View First n Rows of Specific Column
The following code shows how to view the first five rows of a specific column in a DataFrame:
#view first five rows of values in 'points' column
df['points'].head()
0 25
1 12
2 15
3 14
4 19
Name: points, dtype: int64
Example 4: View First n Rows of Several Columns
The following code shows how to view the first five rows of several specific columns in a DataFrame:
#view first five rows of values in 'points' and 'assists' columns
df[['points', 'assists']].head()
points assists
0 25 5
1 12 7
2 15 7
3 14 9
4 19 12The following tutorials explain how to perform other common functions in pandas:
Cite this article
stats writer (2024). How can the head() function in Pandas be utilized and what are some examples of its usage?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-head-function-in-pandas-be-utilized-and-what-are-some-examples-of-its-usage/
stats writer. "How can the head() function in Pandas be utilized and what are some examples of its usage?." PSYCHOLOGICAL SCALES, 5 May. 2024, https://scales.arabpsychology.com/stats/how-can-the-head-function-in-pandas-be-utilized-and-what-are-some-examples-of-its-usage/.
stats writer. "How can the head() function in Pandas be utilized and what are some examples of its usage?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-head-function-in-pandas-be-utilized-and-what-are-some-examples-of-its-usage/.
stats writer (2024) 'How can the head() function in Pandas be utilized and what are some examples of its usage?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-head-function-in-pandas-be-utilized-and-what-are-some-examples-of-its-usage/.
[1] stats writer, "How can the head() function in Pandas be utilized and what are some examples of its usage?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can the head() function in Pandas be utilized and what are some examples of its usage?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
