Page 22 - Informatics_Practices_Fliipbook_Class12
P. 22
C T 03 Write Python code to add record of employee Sanyam with id 'E008' and Salary 80500 at
the end of the series: empNames and empSalary.
append(): To combine two series.
The labels associated with values in a Pandas Series do not need to be unique.
1.5 Operations on Series Objects
Let's consider a Pandas Series that contains the maximum sanctioned strength for seven sections of Class XI:
>>> import pandas as pd
>>> sectionNames = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
>>> sectionStrength = [35, 50, 60, 55, 45, 40, 55]
>>> sections = pd.Series(sectionStrength, index = sectionNames)
>>> print(sections)
output:
A 35
B 50
C 60
D 55
E 45
F 40
G 55
dtype: int64
1.5.1 Retrieving First n and Last n Records
Sometimes, we want to have a look at some data in the beginning or towards the end of a series. For this purpose,
Pandas provides two method, namely, head() and tail(). Let us make use of these methods to display the
sanctioned strength of some sections from the series sections. Although, by default, these methods return five
values, one can also specify the desired number of values as an argument, as shown below:
>>> print("\nStrengths of first 5 sections:\n",sections.head())
output:
Strengths of first 5 sections:
A 35
B 50
C 60
D 55
E 45
dtype: int64
>>> print("\nStrengths of last 5 sections:\n",sections.tail())
output:
Strengths of last 5 sections:
C 60
D 55
E 45
F 40
G 55
dtype: int64
>>> nSections = 3
>>> print("\nStrength of first", "sections:\n",sections.head(nSections))
8 Touchpad Informatics Practices-XII

