0% found this document useful (0 votes)
446 views16 pages

Series in Pandas

The document provides examples of creating Pandas Series from dictionaries, lists, arrays and other data structures. It includes examples of creating Series with default indexes, custom indexes, performing operations on Series like addition, subtraction etc. and accessing/modifying Series elements. It also provides solutions to questions on creating Series, performing operations on them and accessing/modifying elements.

Uploaded by

Anu Soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
446 views16 pages

Series in Pandas

The document provides examples of creating Pandas Series from dictionaries, lists, arrays and other data structures. It includes examples of creating Series with default indexes, custom indexes, performing operations on Series like addition, subtraction etc. and accessing/modifying Series elements. It also provides solutions to questions on creating Series, performing operations on them and accessing/modifying elements.

Uploaded by

Anu Soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

SERIES IN PANDAS

Q1. Create the following Series:

i. EngDict from a dictionary containing 5 words and their meanings. Make


words the index labels and there meaning as the corresponding value.
ii. StateLanguage from a dictionary containing information of at least 4
states and the language spoken in that state. Make state names as
indexes and the language spoken as the corresponding value.
iii. MobileGeneration from a dictionary containing mobile generations as
indexes and their key feature as the corresponding value.
iv. MysqlText from an array containing names of all MySQL text function
with default index values.
v. RollStud from an array containing names of all your classmates with
index labels from 1 onwards.
vi. MarksStud from a scalar value 50 for 10 students.

 Q2. Create the following series:

1. Storing even numbers from 1 to 20 with default indexes


2. Storing following percentages of 5 students stored in the form of a list
with indexes from 1 to 5.
3. 90, 85, 78.5, 90.5, 56.5
4. Storing value 100 with indexes as odd numbers from 1 to 10
5. Storing value “Pass and Promoted” for 5 students with names: Ajay,
Vikas, Sunesh, Rita, Ramya
6. Store names of the students as index of the series
7. Storing numbers from 1 to 5 as indexes and their square values as
data.
8. Consider list – MonthNames, containing names of months and list-
NoOfDays, containing number of days in each month. Create a series
with month names as indexes and number of days as data
corresponding to each index.

Q3. Consider the following dictionary:

states={'A':'Andhra Pradesh', 'B':'Bihar', 'D':'Delhi', 'K':'Kerala', 'R':'Rajasthan',


'T':'Tamil Nadu'}

Create a series from the above dictionary and write Python statements for the
following:

i. To display 1st,3rd,5th elements of the series.


ii. To display all elements from 2nd to 5th present in the series.
iii. To replace the value of index ‘K’ by ‘Karnataka’.
iv. To assign value ‘My Pride’ to 3rd - 6th elements
v. To display first three elements of the series
vi. To display last five elements of the series
vii. To display the series in reverse order
Q4. Create two lists one storing name of the country and the other one storing
the capitol of that country. Create a series s1 from these lists with country
names as indexes. Answer the following questions:

  Command   Command
>>>print(s1[-3:]) >>>print(s1.index)
i) vi)
   
>>>print(s1[:-4]) >>>print(s1.shape)
ii) vii)
   
>>>print(s1[::-2]) >>>print(s1.dtype)
iii) viii)
   
>>>print(s1.size) >>>print(s1.empty)
iv) ix)
   
>>>print(s1.hasnans) >>>print(s1.ndim)
v) x)
   

Q5. Consider an array with values: 10,20,30,40,50

Create a series from this array with default indexes and write Python
statements for the following:

i. Set the values of all elements to 100


ii. Create a new series with index values from ‘A’ to “E’
iii. Add 10 to all elements of the series and display it
iv. Display 1st and 4th elements of the series
v. Display 1st to 3rd elements of the series
vi. Set the value of 4th element to 200

Q6. Consider the following two series:

ser1=pd.Series([2,5,7,9,6], index=['A','B','C','D','E'])

ser2=pd.Series([8,2,6,3,9], index=['A','K','D','M','E'])

Write Python statements to perform the following operations. Also give output
that will be obtained after performing the desired operation.

i. Add ser1 to ser2


ii. Add ser2 to ser1 filling missing indexes with value 10.
iii. Subtract ser1 from ser2
iv. Subtract ser2 from ser1 filling missing indexes with value 5.
v. Multiply ser1 with ser2
vi. Multiply ser2 with ser1 filling missing index values with 0
vii. Divide ser1 by ser2
viii. Divide ser2 by ser1 filling missing index values with 5.

SOLUTIONS

1.Create the following Series:

i) 

import pandas as pd  

d={'Cognizant': 'Awareness', 'Construe': 'Interpret', 'Envisage': 'to
imagine', 'Gullible': 'Easily deceived', ‘Dichotomy’: ‘A contrast between two
things’} 

s1=pd.Series (d) 

print(s1) 

  ii)

Import pandas as pd 

d1={'Kerla': 'Malayalam' ,'Gujrat': 'Gujrati, 'Haryana': 'Haryanvi, ‘Punjab':


'Punjabi'} 

s1=pd.Series(d1) 

print(s1) 

OUTPUT

Kerala         Malayalam 

Gujrat        Gujrati 

Haryana     Haryanvi 

Punjab      Punjabi 

iii)

 
Import pandas as pd 

d1={'1 Generation':'vacuum Tube','2 Generation': 

'2G enabled','3 Generation':'Mobile internet
access', '4 Generation':'Video Conferencing','5 Generation':'faster data delivery
rate'} 

s1=pd.Series(d1) 

OUTPUT

1st Generation           vacuum Tube 

2nd Generation         2G enabled

3rd Generation          Mobile internet access 

4th Generation          Video Conferencing 

5th Generation          faster data delivery rate

 2. Create the following series:

i)

import pandas as pd 

import numpy as np 

evenno=np.arange(2,21,2) 

s=pd.Series(evenno) 

print(s) 

ii)

import pandas as pd 

import numpy as np 

L=[90,85,78.5,90.5,56.5] 
s=pd.Series(L) 

print(s) 

OUTPUT

1    85.0 

2    78.5 

3    90.5 

4    56.5 

iii)

import pandas as pd 

import numpy as np 

a=np.arange(1,10,2) 

b=pd.Series(100,index=[a]) 

print(b) 

OUTPUT

1    100 

3    100 

5    100 

7    100 

9    100 

iv)

import pandas as pd 

import numpy as np 

a=np.array(['Ajay','Vikash','Sunesh','Rita','Ramya']) 
b=pd.Series('Pass and Promoted',index=[a]) 

print(b) 

OUTPUT

Ajay      Pass and Promoted 

Vikash    Pass and Promoted 

Sunesh    Pass and Promoted 

Rita      Pass and Promoted 

Ramya     Pass and Promoted 

iv)

import pandas as pd 

import numpy as np 

a=np.arange(1,5) 

b=a*a 

c=pd.Series(b,index=[a]) 

print(c) 

OUTPUT

1     1 

2     4 

3     9 

4    16 

V)

import pandas as pd 

import numpy as np 


a=np.array(['Jan','Feb','Mar''Apr','May','Jun','July','Aug','Sep','Oct','Nov','Dec']) 

b=np.array([31,30,31,30,31,30,31,31,30,31,30,31]) 

c=pd.Series(b,index=[a]) 

print(c) 

OUTPUT

Jan     31 

Feb     30 

Mar     31 

Apr     30 

May     31 

Jun     30 

July    31 

Aug     31 

Sep     30 

Oct     31 

Nov     30 

Dec     31 

 3. Consider the following dictionary:

states={'A':'Andhra Pradesh', 'B':'Bihar', 'D':'Delhi', 'K':'Kerala',


'R':'Rajasthan', 'T':'Tamil Nadu'}

Create a series from the above dictionary and write Python statements for
the following:

a)

import pandas as pd 

states={'A':'Andra pradesh','B':'Bihar','D':'Delhi', 
'k':'keerla','R':'Rajasthan','T':'Tamil'} 

s=pd.Series(states) 

print(s[[0,2,4]]) 

OUTPUT

A    Andra pradesh 

d            Delhi 

r        Rajasthan 

b)

import pandas as pd 

states={'A':'Andra pradesh','B':'Bihar','D':'Delhi', 

'k':'keerla','R':'Rajasthan','T':'Tamil'} 

s=pd.Series(states) 

print(s[2:]) 

OUTPUT

d        Delhi 

k       Keerla 

r    Rajasthan 

t        Tamil 

c)

import pandas as pd 

states={'A':'Andra pradesh','B':'Bihar','D':'Delhi', 

'k':'keerla','R':'Rajasthan','T':'Tamil'} 
s=pd.Series(states) 

s[3]='karnataka' 

print(s['k']) 

OUTPUT

K          karnataka 

d)

import pandas as pd 

states={'A':'Andra pradesh','B':'Bihar','D':'Delhi', 

'k':'keerla','R':'Rajasthan','T':'Tamil'} 

s=pd.Series(states) 

s[[2,5]]='My pride' 

print(s) 

OUTPUT

A    Andra pradesh 

B            bihar 

D         My pride 

K           keerla 

R        rajasthan 

T         My pride 

e)

import pandas as pd 

states={'A':'Andra pradesh','B':'Bihar','D':'Delhi', 
'k':'keerla','R':'Rajasthan','T':'Tamil'} 

s=pd.Series(states) 

print(s.head(3)) 

OUTPUT

A    Andra pradesh 

B            bihar 

D            delhi 

f)

import pandas as pd 

states={'A':'Andra pradesh','B':'Bihar','D':'Delhi', 

'k':'keerla','R':'Rajasthan','T':'Tamil'} 

s=pd.Series(states) 

print(s.tail(5)) 

OUTPUT

A    Andra pradesh 

B            bihar 

D            delhi 

K           keerla 

R        rajasthan 

g)

import pandas as pd 

states={'A':'Andra pradesh','B':'Bihar','D':'Delhi', 
'k':'keerla','R':'Rajasthan','T':'Tamil'} 

s=pd.Series(states) 

print(s[::-1]) 

OUTPUT

T            tamil 

R        rajasthan 

K           kerla 

D            delhi 

B            bihar 

A    Andra pradesh 

4. Create two lists one storing name of the country and the other one
storing the capital of that country. Create a series s1 from these
lists with country names as indexes. Answer the following
questions:

  Command
>>>print(s1[-3:])

OUTPUT

BHUTAN          TIMPHU 
i)
NEPAL        KATHMANDU 

AUSTRALIA    
CANBERRA 

 
ii) >>>print(s1[:-4])
OUTPUT

INDIA    NEW DELHI 

 
>>>print(s1[::-2])

OUTPUT

AUSTRALIA    
CANBERRA 
iii)
BHUTAN          TIMPHU 

INDIA        NEW DELHI 

 
>>>print(s1.size)

iv) OUTPUT

5
>>>print(s1.hasnans)

OUTPUT
v)
False 

 
vi) >>>print(s1.index)

OUTPUT

[MultiIndex([(    'INDIA',), 

            (  'AMERICA',), 

            (   'BHUTAN',), 

            (    'NEPAL',), 

            ('AUSTRALIA',)], 
           )] 

 
>>>print(s1.shape)

OUTPUT
vii)
(5,) 

 
>>>print(s1.dtype)

OUTPUT
viii)
Object 

 
>>>print(s1.empty)

OUTPUT
ix)
False 

 
>>>print(s1.ndim)

OUTPUT
x)

5. Consider an array with values: 10,20,30,40,50

Create a series from this array with default indexes and write Python
statements for the following:

a)

import pandas as pd 

import numpy as np 

a=np.array([10,20,30,40,50]) 
b=pd.Series(a) 

a[[0,1,2,3,4]]=100 

print(b) 

OUTPUT

0    100 

1    100 

2    100 

3    100 

4    100 

b)

import pandas as pd 

import numpy as np 

a=np.array([10,20,30,40,50]) 

b=pd.Series(a,index=['a','b','c','d','e']) 

print(b) 

       

OUTPUT

a    10 

b    20 

c    30 

d    40 

e    50 

c)
import pandas as pd 

import numpy as np 

a=np.array([10,20,30,40,50]) 

b=pd.Series(a,index=['a','b','c','d','e']) 

b[['a','b','c','d','e']]=10 

print(b) 

OUTPUT

a    10 

b    10 

c    10 

d    10 

e    10 

d)

import pandas as pd 

import numpy as np 

a=np.array([10,20,30,40,50]) 

b=pd.Series(a,index=['a','b','c','d','e']) 

print(b[[0,3]]) 

OUTPUT

a    10 

d    40 

e)

import pandas as pd 
import numpy as np 

a=np.array ([10,20,30,40,50]) 

b=pd.Series (a,index=['a','b','c','d','e']) 

print(b[[0,2]]) 

  

OUTPUT

a    10 

c    30 

f)

import pandas as pd 

import numpy as np 

a=np.array([10,20,30,40,50]) 

b[3]=200 

print(b) 

OUTPUT

a     10 

b     20 

c     30 

d    200 

e     50  

You might also like