Pandas Series is a type of data object that stores labeled information. A Series can be created from by inputting a Python dictionary, which stores key-value pairs information. The dictionary’s key then becomes the label of the resulting Series, and value of the dictionary will be the value of the Series. Alternatively, the label of Series can be manually set than using either the default label from Series or from the key information in the dictionary. In such case, if the manually specified label does not match the value part in the dictionary, the resulting Series will have NaN(not a number) value. Next example codes show how to implement these operations in Python IDE.
#Import Pandas module
import pandas as pd
#Create a dictionary
D1 = {'home': 'Molde', 'age': 25, 'gender': 'male', 'color': 'green'}
S1 = pd.Series(D1)
#Create a Series from inputting dictionary
S1
#output
home Molde
age 25
gender male
color green
dtype: object
#Create a list representing labels for Series
L1 = ['home', 'age', 'gender', 'color', 'height']
#Create a Series, by inputting dictionary and label list
S2 = pd.Series(D1, index=L1)
S2
#output, there is a NaN value in the Series
#because the label list and values in dictionary not match
home Molde
age 25
gender male
color green
height NaN
dtype: object
#We create a new list for labels, but change sequence
L2 = ['color', 'age', 'gender', 'home', 'height']
#then create a Series from inputting dictionary and
#using new label list
S3 = pd.Series(D1, index=L2)
S3
#output, seems sequence of label has no effect, because
#Pandas will find the labels and assign corresponding values
#in the dictioanry to the Series
color green
age 25
gender male
home Molde
height NaN
dtype: object
Click here to download Python Course Source Files !
For online Python training registration, click here ! Pandas provides flexible ways of generating data…
For online Python training registration, click here ! Data frame is the tabular data object…
Click her for course registration ! When a data frame in Python is created via…
We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…