Explain how series data structure in Python can be created using scalar/constant values?


Scalar or constant values are defined once, and they are repeated across all rows/entries of the series data structure. Following is an example −

Example

 Live Demo

import pandas as pd
my_index = ['ab', 'mn' ,'gh','kl']
my_series = pd.Series(7, index = my_index)
print("This is series data structure created using scalar values and specifying index values")
print(my_series)

Output

This is series data structure created using scalar values and specifying index values
ab   7
mn   7
gh   7
kl   7
dtype: int64

Explanation

  • The required libraries are imported, and their alias are given so that it is easy to use them.
  • A list of index values are created.
  • A constant value is passed to ‘Series’ function present in the ‘pandas’ library.
  • Along with it, the index list is also passed.
  • It is then displayed on the console.

If the index values are not customized, default values beginning from 0 are taken. Since, only one constant value is specified, there will be a single entry in the series data structure. It has been demonstrated below −

Example

 Live Demo

import pandas as pd
my_series = pd.Series(7)
print("This is series data structure created using scalar values and default index values")
print(my_series)

Output

This is series data structure created using scalar values and default index values
0  7
dtype: int64

Explanation

  • The required libraries are imported, and their alias are given so that it is easy to use them.

  • A constant value is passed to ‘Series’ function present in the ‘pandas’ library.

  • It is then displayed on the console.

Updated on: 10-Dec-2020

190 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements