- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the use of head () methods in Pandas series?
The head() method in the pandas series is used to retrieve the topmost rows from a series object. By default, it will display 5 rows of series data, and we can customize the number of rows other than 5 rows.
This method takes an integer value as a parameter to return a series with those many rows, suppose if you give integer n as a parameter to the head method like head(n) then it will return a pandas series with n number of elements. And those elements are the first n number of elements of our pandas series object.
Example
# importing required packages import pandas as pd import numpy as np # creating pandas Series object series = pd.Series(np.random.rand(20)) print(series) print('
The Resultant series from head() method:') # Displaying top row from series data print(series.head())
Explanation
In this example, we have created a pandas series object ‘series’ with all randomly generated values by using the NumPy package.
Here we define an integer value 20 to the “np.random.rand” method to get 20 random values as a parameter to our pandas.series function. Hence the total number of elements present in this series object is 20 and index labels are positional index values that are automatically generated by the pandas series function.
Output
0 0.109458 1 0.199291 2 0.007854 3 0.205035 4 0.546315 5 0.442926 6 0.897214 7 0.950229 8 0.494563 9 0.843451 10 0.582327 11 0.257302 12 0.915850 13 0.364164 14 0.388809 15 0.918468 16 0.598981 17 0.631225 18 0.555009 19 0.684256 dtype: float64 The Resultant series from head() method: 0 0.109458 1 0.199291 2 0.007854 3 0.205035 4 0.546315 dtype: float64
The above output block has two outputs one is the actual pandas series object ‘series’ and the second is an output of top elements from the series object (series) which are displayed by the head() method.
Example
import pandas as pd # creating dates date = pd.date_range("2021-01-01", periods=15, freq="M") # creating pandas Series with date index S_obj = pd.Series(date, index=date.month_name()) print(S_obj) print('
The Resultant series from head() method:') # Displaying top row from series data print(S_obj.head(1))
Explanation
In the first example, we have seen the default output of the head method, it retrieves 5 elements from the series object by default. And we can get the desired number of output elements from the head method by sending an integer parameter to the head function.
Output
January 2021-01-31 February 2021-02-28 March 2021-03-31 April 2021-04-30 May 2021-05-31 June 2021-06-30 July 2021-07-31 August 2021-08-31 September 2021-09-30 October 2021-10-31 November 2021-11-30 December 2021-12-31 January 2022-01-31 February 2022-02-28 March 2022-03-31 dtype: datetime64[ns] The Resultant series from head() method: January 2021-01-31 dtype: datetime64[ns]
In this example, we have sent an integer value 1 to the head method as a parameter. And we can see the result in the above output block it returns an element which is the topmost element from our series object.