Page 20 - Informatics_Practices_Fliipbook_Class12
P. 20
Series: One-dimensional data structure of Pandas module that can accommodate objects of various types.
1.3 Typecasting
The method astype() is used for typecasting the type of objects in a series. For example, while marks of some students
may appear as integers, those of others may appear as fractions. We can transform them all as integers as shown below:
01 import pandas as pd
02 marks = [90.5, 70, 95, 45, 100]
03 rollNumbers = [101, 102 , 103, 105, 106]
04 students = pd.Series(marks, index = rollNumbers)
05 print('Before Typecasting:')
06 print(students)
07 students = students.astype(int)
08 print('After Typecasting:')
09 print(students)
output:
Before Typecasting:
101 90.5
102 70.0
103 95.0
105 45.0
106 100.0
dtype: float64
After Typecasting:
101 90
102 70
103 95
105 45
106 100
dtype: int32
C T 02 Write a Python code to transform the salaries of employees stored in empSalary to floating
point value.
astype(): Typecasting the type of objects in a series.
1.4 Using Dictionary to Create Series Object
Suppose, you want to create a series of national sports of some countries, indexed by the name of the country. As
Pandas allows us to transform a dictionary to a series, a dictionary becomes the natural choice for such situations, as
illustrated below:
>>> import pandas as pd
>>> sportsDict = {'Bhutan': 'Archery',
... 'Scotland': 'Golf',
... 'Japan': 'Sumo',
... 'India': 'Hockey' } #Creating Series from Dictionary
6 Touchpad Informatics Practices-XII

