Series-2 Ip
Series-2 Ip
>>>import pandas as pd
>>>d1 = dict{“Jan” : 1, “Feb” : 2, “Mar” : 3}
>>>s8 = pd.Series(d1)
>>>s8
Jan 1
Feb 2
Mar 3
dtype int32
• When you convert a dictionary into series, then keys of the dictionary
become index of the series, and value of the dictionary become the data of
the series.
• The indexes which are created from keys may not be in the same order as
you have typed them.
Example : Write a program to create a Series object using a dictionary that
stores the number of students in each section of class 12 in your school.
>>>import pandas as pd
>>>stud = {‘A’ : 39, ‘B’ : 42, ‘C’ : 38, ‘D’ : 41}
>>>s9 = pd.Series(stud)
>>>s9
A 39
B 42
C 38
D 41
dtype int 64
Specify data as a Scalar value : The data can be in the form of a single value
(scalar value).
• If the data is a scalar value, then the index argument to Series() must be
provided.
• The scalar value will be repeated to match the length of the index.(The
index argument can be a sequence of numbers or labels of any type).
>>>import pandas as pd
>>>s1 = pd.Series(10, index=range(0,2))
>>>s1
0 10
1 10
dtype : int64
Example 2
>>>import pandas as pd
>>>s2 = pd.Series(15, index = range(1, 6, 2))
>>>s1
1 15
3 15
5 15
dtype : int64
Example 3
>>>import pandas as pd
>>>s3 = pd.Series(‘Yet to Start’, index = [‘Indore’, ‘Delhi’, ‘Simla’]
>>>s3
Indore Yet to Start
Delhi Yet to Start
Simla Yet to Start
dtype : object
Example 4 : Write a program to create a Series object that stores the initial
budget allocated 50000 each for the four quarters of the year : Qtr1, Qtr2,
Qtr3, Qtr4
>>>import pandas as pd
>>>s4 = pd.Series(50000, index = [‘Qtr1’, ‘Qtr2’, ‘Qtr3’, ‘Qtr4’])
>>>s4
Qtr1 50000
Qtr2 50000
Qtr3 50000
Qtr4 50000
dtype : int
Example 5 : Total number of medals to be won is 200 in the Inter University
games held every alternate year. Write code to create a Series object that
stores these medals for games to be held in the decade 2020 – 2029.
>>>import pandas as pd
>>>s5 = pd.Series(200, index = arange(2020, 2029,2)
>>>s4
200 2020
200 2022
200 2024
200 2026
200 2028
dtype : int64
Specify index(es) as well as data with Series()
While creating Series type object is that along with values, you also provide
indexes. Both values and indexes are sequences.
Example
>>>s1 = pd.Series(data = [32, 34, 35], index=[‘A’, ‘B’, ‘C’])
>>>s1
A 32
B 34
C 35
dtype int64
Evenif we skip the keyword data, it will create the Series.