You are on page 1of 6

INFORMATICS PRACTICES CLASS 12 2020-21

Worksheet on Pandas Series


Q1. Write a python code to create a series object Temp1 that stores temperatures
of seven days in it. Take any random seven temperatures.
Ans: import pandas as pd
Temp1=pd.Series([34.0,29.2,39.4,37.7,39.4])
print(Temp1)

Q2. Write a python code to create a series object Temp1 (from array ) that
stores temperatures of seven days of week. Its indexes should be „Sunday‟,
„Monday‟, . . . „Saturday‟.
Ans: import numpy as np
import pandas as pd
n=np.array([34.0,29.2,39.4,37.7,39.4])
Temp1=pd.Series(n, index=[‘sun’,’mon’,’tue’,’wed’,’thur’])
print(Temp1)
OR
Temp1=pd.Series(data=n, index=[‘sun’,’mon’,’tue’,’wed’,’thur’])
OR
Temp1=pd.Series( index=[‘sun’,’mon’,’tue’,’wed’,’thur’],data=n)

Q3. Write commands to do the following:


(a) Create empty series:
import pandas as pd
m=pd.Series()
print(m)
(b) Indexes of the series.
import pandas as pd
import numpy as np
n=np.array([34.0,29.2,39.4,37.7,39.4])
Temp1=pd.Series(n, index=[‘sun’,’mon’,’tue’,’wed’,’thur’])
print(Temp1.index)

(c) Data type of the series


import pandas as pd
import numpy as np
n=np.array([34.0,29.2,39.4,37.7,39.4])
Temp1=pd.Series(n, index=['sun','mon','tue','wed','thur'])
print(Temp1.dtype)
print(type(n))
print(type(Temp1))
(d) print the entire series
print(nameofSeries)
NOTE: Difference between the dtype attribute and type( ) function of
python
Q4. Create two series s1 and s2. S1 contains 3 values and s2 contains 5 values.
Perform addition of s1 and s2. Do we have NAN values in them? ( chk with
s.hasnans)
Ans(i) Yes I will have NAN values in my result.
Ex: import pandas as pd
s1=pd.Series([20,30,40])
s2=pd.Series([2,3,4,5,6])
print(s1+s2)

(ii) import pandas as pd


s1=pd.Series([20,30,40])
s2=pd.Series([2,3,4,5,6])
s=s1+s2
print(s.hasnans)
NOTE: hasnans is an attribute of series, which checks whether the
mentioned series has NAN values in it or not. If yes, it returns True or else
it returns False
Q5. Consider the following Series object namely S:
0 0.430271
1 0.617328
2 -0.265421
3 -0.836113
What will be returned by following statements?
(a) S *100
Output:
0 43.0271
1 61.7328
2 -26.5421
3 -86.1130
dtype: float64
(b) S>0
Output:
0 True
1 True
2 False
3 False
dtype: bool
(c)S1 = pd.Series(S)
OutPut:
No output displayed but it creates another series S1 containing same
elements as of s

(c) S2=pd.Series(S1) + 3
No output is displayed. But
s1
0 0.430271
1 0.617328
2 -0.265421
3 -0.861130
dtype: float64
>>> s2
0 3.430271
1 3.617328
2 2.734579
3 2.138870
dtype: float64
(d) S[S>0]
Output:
0 0.430271
1 0.617328

Q6. Give the output of the following code:


Stationary =[‘pencils’,’notebooks’,’scales’, ‘erasers’]
S1=pd.Series([20,23,45,60], index=Stationery)
S2= pd.Series([30,40,50,70], index=Stationery)
print(S1+S2)
s=S1+S2
print(s+S1)
Output:
pencil 50
notebooks 63
scales 95
erasers 130
dtype: int64

pencil 70
notebooks 86
scales 140
erasers 190
dtype: int64
Q7. What will be the output produced by following code, considering the Series
object S given in above question.
Ans:
So the content of s is :
pencil 50
notebooks 63
scales 95
erasers 130

(a) print(s[1:1]
Ans: no content……so it displays a blank series

Series([], dtype: int64)

(b) print(s[0:1])
pencil 50
dtype: int64

(c) print(s[0:2])
pencil 50
notebooks 63
dtype: int64

(d) s[0:2]=12
no output displayed, but the values at index 0 and 1 are changed to 12
ie. pencil and notebook values become 12. So when we print s, after the
change we get:
pencil 12
notebooks 12
scales 95
erasers 130
dtype: int64
(e) print(s.index)
Index(['pencil', 'notebooks', 'scales', 'erasers'], dtype='object')
(f) print(s)
(g) print(s.values)
[12 12 95 130]

Q8. Find the error and correct:


data=np.array([‘a’,’b’,’c’,’d’,’e’,’f’])
s=pd.Series(index=[100,101,102,103,104,105])
print(s[102,103,104])

Corrected code:
import numpy as np
import pandas as pd
data=np.array(['a','b','c','d','e','f'])
s=pd.Series(data,index=[100,101,102,103,104,105]) # Creation of series without data
will not give a syntax error. It
print(s[[102,103,104]]) creates a blank series. Here we have
mentioned the index, so indexes
with NAN values
Q9. if Ser is a Series type object having 30 values,
then how are statements (a), (b) , (c) and (d) similar
and different?
(a) print(Ser.head()): displays the first five record of the series
(b) print(Ser.head(8)) : displays the first 8 records of the series
(c) print(Ser.tail()) : displays the last five records of the series
(d) print(Ser.tail(11)): displays the last 11 records of the series
Q10. Write a program to create a series to store the number of students enrolled
in 8 games during summer camp. The game names are the indexes and the
number of students as the data. The data is stored in a python list.
Ans: import pandas as pd
list1=[25,30,15,18,12,27,16,20]
s=pd.Series(list1,index=['swimming','tt','skating','kho
kho','bb','chess','football','cricket'])
print(s)
Output:
swimming 25
tt 30
skating 15
kho kho 18
bb 12
chess 27
football 16
cricket 20
dtype: int64

Q11. Write the python code to print the last 2 records of the above created
series.
Ans:
import pandas as pd
list1=[25,30,15,18,12,27,16,20]
s=pd.Series(list1,index=['swimming','tt','skating','kho kho', 'bb', 'chess',
'football', 'cricket'])
print(s)
print(s.tail(2))
print(s[['football','cricket']])
print(s[6:8])
Q12. Write the python code to print the last 5 records of the above created
series.
print(s.tail( ))
Q13. Write a program to create a dictionary to store names and age of 5
students. Use this dictionary to create a series and print it.
import pandas as pd
dict1={'ujjwal':17,'sushant':16,'manonita':15,'aryan':7,'shreya':18}
s=pd.Series(dict1)
print(s)

You might also like