Let us see how a series data structure can be created with the help of a Numpy array, and explicitly giving values for ‘index’.
When no value is specified for index, default values beginning from 0 are assigned to values in the series.
Following is an example −
Example
import pandas as pd import numpy as np my_data = np.array(['ab','bc','cd','de', 'ef', 'fg','gh', 'hi']) my_index = [3, 5, 7, 9, 11, 23, 45, 67] my_series = pd.Series(my_data, index = my_index) print("This is series data structure created using Numpy array and specifying index values") print(my_series)
Output
This is series data structure created using Numpy array and specifying index values 3 ab 5 bc 7 cd 9 de 11 ef 23 fg 45 gh 67 hi dtype: object
Explanation
The required libraries are imported, and given alias names for ease of use.
The next step is to create a numpy array structure.
Next, a list of values that needs to be explicitly specified as index is created.
To the ‘Series’ function present in the ‘pandas’ library, the previously created data and index list is passed as parameters.
The output is displayed on the console.
Note − This indicates that customized values for index can be specified while creating a series data structure.