Pandas Series.head() Function

Pandas Series.head() Function

Minahil Noor Feb-28, 2021 Feb-14, 2021 Pandas Pandas Series
  1. Syntax of pandas.Series.head():
  2. Example Codes: Series.head() Method to Return First 5 Values of a Series
  3. Example Codes: Series.head() Method to Return First 7 Values of a Series
  4. Example Codes: Series.head() Method to Return an Empty Series

Python Pandas Series.head() function returns a number of selected values from the start.

Syntax of pandas.Series.head():

Series.head(n=5)

Parameters

n It is an integer parameter. It specifies the number of values to select and return.

Return

It returns the first n values of the Series.

Example Codes: Series.head() Method to Return First 5 Values of a Series

import pandas as pd

series = pd.Series([ 'Rose', 'Jasmine', 'Lili', 'Tulip', 'Hibiscus', 'Sun Flower', 'Orchid', 'Carnation','Irises', 'Gardenias'])
print("The Original Series is: \n")
print(series)

series1 = series.head()
print("The First Five Values of Series From Start are: \n")
print(series1)

Output:

The Original Series is: 

0          Rose
1       Jasmine
2          Lili
3         Tulip
4      Hibiscus
5    Sun Flower
6        Orchid
7     Carnation
8        Irises
9     Gardenias
dtype: object
The First Five Values of Series From Start are: 

0        Rose
1     Jasmine
2        Lili
3       Tulip
4    Hibiscus
dtype: object

We have passed no parameter as the default value of n is 5. The function has returned the first five values of the given Series.

Example Codes: Series.head() Method to Return First 7 Values of a Series

import pandas as pd

series = pd.Series([ 'Rose', 'Jasmine', 'Lili', 'Tulip', 'Hibiscus', 'Sun Flower', 'Orchid', 'Carnation','Irises', 'Gardenias'])
print("The Original Series is: \n")
print(series)

series1 = series.head(7)
print("The First Seven Values of Series From Start are: \n")
print(series1)

Output:

The Original Series is: 

0          Rose
1       Jasmine
2          Lili
3         Tulip
4      Hibiscus
5    Sun Flower
6        Orchid
7     Carnation
8        Irises
9     Gardenias
dtype: object
The First Seven Values of Series From Start are: 

0          Rose
1       Jasmine
2          Lili
3         Tulip
4      Hibiscus
5    Sun Flower
6        Orchid
dtype: object

We have passed n= 7. The function has returned the first seven values of the given Series.

Example Codes: Series.head() Method to Return an Empty Series

import pandas as pd

series = pd.Series([ 'Rose', 'Jasmine', 'Lili', 'Tulip', 'Hibiscus', 'Sun Flower', 'Orchid', 'Carnation','Irises', 'Gardenias'])
print("The Original Series is: \n")
print(series)

series1 = series.head(0)
print("The Empty Series is: \n")
print(series1)

Output:

The Original Series is: 

0          Rose
1       Jasmine
2          Lili
3         Tulip
4      Hibiscus
5    Sun Flower
6        Orchid
7     Carnation
8        Irises
9     Gardenias
dtype: object
The Empty Series is: 

Series([], dtype: object)

The function has returned an empty series.

Related Article - Pandas Series

  • Pandas Series.to_frame() Function
  • Pandas Series.tolist() Function
  • Pandas Series Series.map() Function
  • Pandas Series Series.nunique() Function
  • Pandas Series Series.unique() Function