Page 16 - Informatics_Practices_Fliipbook_Class12
P. 16
2 Bear
3 Lion
dtype: object
Note that as the class Series is part of the Package, we write pd.Series to refer to the class Series. As the names
of the animals are already availble in the list animals, we used animals as the input argument. Further, note that
the description of the object animalSeries includes row indexes, often called row labels. The row labels 0, 1, 2, ...
are used to refer to the associated values animalSeries[0], animalSeries[1], animalSeries[2], ...,
as shown below:
>>> animalSeries[3]
output:
'Lion'
We can find out all the valid indexes for a series as follows:
>>> animalSeries.index
output:
RangeIndex(start=0, stop=4, step=1)
Similarly, we can display all the values in a series as follows:
>>> animalSeries.values
output:
array(['Elephant', 'Tiger', 'Bear', 'Lion'], dtype=object)
In the absence of a list comrising the names of the animals, we could create a series by providing the names of the
animals in the form of list or a tuple, as the argument as shown below:
>>> animalSeries = pd.Series(['Elephant', 'Tiger', 'Bear', 'Lion'])
>>> print(animalSeries)
0 Elephant
1 Tiger
2 Bear
3 Lion
dtype: object
Similarly, let us define another series object marksSeries comprising marks of five students created using a list
marks as follows:
>>> marks = [90, 70, 95, 45, 100]
>>> marksSeries = pd.Series(marks)
>>> print(marksSeries)
output:
0 90
1 70
2 95
3 45
4 100
dtype: int64
A series object such as marksSeries can also be created using a numpy array comprising marks of five students as
follows:
>>> import pandas as pd
>>> import numpy as np
>>> marks = np.array([90, 70, 95, 45, 100]) #Creating Pandas Series using numpy array
>>> marksSeries = pd.Series(marks)
>>> print(marksSeries)
0 90
1 70
2 95
3 45
4 100
dtype: int64
When a Pandas Series contains objects of different types, Pandas makes an effort to cast all the objects to a common type.
2 Touchpad Informatics Practices-XII

