Professional Documents
Culture Documents
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)
(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
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
(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]
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)