Python Pandas - 1 || Type B || Sumita Arora || Class 12 || Information Practices || Solution



Q1. Consider following Series object namely S:

0     0.430271
1     0.617328
2     -0.265421
3     -0.836113

dtype: float64
What will be returned by following statements?
(a) S * 100
(b) S > 0
(c) S1 = pd.Series(S)
(d) S3 = pd.Series(S1) + 3
What will be the values of Series objects S1 and S2 created above ?




Q2. Consider the same Series object, S, given in the previous question. What output will be produced by following code fragment?

S.index = ['AMZN', 'AAPL', 'MSFT', ‘GOOG’]
print(S)
print(S['AMZN'])
S['AMZN'] = 1.5
print( S['AMZN'])
print(S)




Q3. What will be the output produced by following code?

Stationery = ['pencils', 'notebooks', 'scales', 'erasers']
S = pd. Series ([20, 33, 52, 10], index = Stationery)
S2 = pd. Series ([17, 13, 31, 32], index = Stationery)
print (S + S2)
S = S + S2
print (S + S2)




Q4. What will be the output produced by following code, considering the Series object S given above?
(a) print(S[1:1])
(b) print(S[0:1])
(c) print (S[0:2])
(d) S[0:2] = 12
print(S)
(e) print(S.index)
print(S.values)



Q5. Find the error in following code fragment:
(a)
S2 = pd. Series( [101, 102, 102, 104] )
print(S2.index)
S2.index = [0, 1, 2, 3, 4, 5]
S2[5] = 220
print(52)
(b) S = pd. Series (2, 3, 4, 5, index = range (4) )
(c) S1 = pd. Series (1, 2, 3, 4, index = range (7))
(d) S2 = pd. Series ([1, 2, 3, 4], index = range (4) )




Q6. Find the Error:

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

Can you correct the error ?




Q7. Why does following code cause error?
s1 = pd. Series (range(1, 15, 3), index = list('abcd'))




Q8. Why does following code cause error?
s1 = pd.Series(range(1, 15, 3), index = list('ababa'))
print(s1['ab'])




Q9. If Ser is a Series type object having 30 values, then how are statements (a), (b) and (c), (d) similar and different?
(a) print(Ser.head( ))
(b) print(Ser.head( 8 ))
(c) print(Ser.tail())
(d) print(Ser.tail(11))




Q10. What advantages does dataframe offer over series datastructure? If you have similar data stored in multiple series and a single dataframe, which one would you prefer and why?




Q11. Given :

import pandas as pd
d = {'one' : pd.series([1., 2., 3.], index = ['a', 'b', 'c']), 'two' : pd.series([1., 2., 3., 4.], index = ['a', 'b', 'c', 'd'])}
df = pd.Dataframe(d)
df1 = pd.Dataframe(d, index = ['d', 'b', 'a'])
df2 = pd. Dataframe(d, index = ('d', 'a'), columns = ['two', 'three'])
print(df)
print(df1)
print(df2)
What will Python show the result as if you execute above code?



Q12. From the dataframes created in previous question, write code to

(a) Display only column 'a' from dataframes df, df1, and df2.
(b) Display only rows 0 and 1 from dataframes df, df1, and df2.
(c) Display only column 'a' and 'b' for rows 1 and 2 from dataframes df, df1 and df2.
(d) add an empty column 'x' to all dataframes.





Questions 13 and 14 make use of this dictionary:

my_di = {"name" : ["Jiya", "Tim", "Rohan"], "age" : np.array([10,15, 20]), "weight": (75,123,239), "height" : [4.5, 5, 6.1], "siblings" : 1, "gender" : "M"}



Q13. Predict the output of following code (it uses above given dictionary my_di)
Df = pd.DataFrame(my_di)
print(af)


Q14. Consider the same dictionary my_di in the previous question, what will be the output produced by following code?

df2 = pd.DataFrame (my_di, index = my_di ["name"])
print (df2)





Using the df2 created in Question 14, answer the outputs of questions (15 to 19) given below.



Q15. Assume that required libraries (panda and numpy) are imported and dataframe df2 has been created as per questions 13 and 14 above. Predict the output of following code fragment:
print ( df2["weight"])
print ( df2.weight['Tim' ] )




Q16. Assume that required libraries (panda and numpy) are imported and dataframe df2 has been created as per questions 13 and 14 above. Predict the output of following code fragment:
df2["IQ"] = [130, 105, 115]
df2["Married"] = False
print ( df2 )




Q17. Assume that required libraries (panda and numpy) are imported and dataframe df2 has been created as per questions 13 and 14 above. Predict the output produced by following code fragment:
df2["College"] = pd. Series (["IIT"], index=["Rohan"] )
print ( df2 )




Q18. Assume that required libraries (panda and numpy) are imported and dataframe df2 has been created as per questions 13 and 14 above. Predict the output produced by following code fragment:

print ( df2.loc["Jiva"] )
print ( df2.loc["Jiva", "IQ"] )
print ( df2.loc["Jiva":"Tim" , "IQ" : "College"])
print (df2.iloc[0] )
print (df2.iloc[0, 5] )
print ( df2f2.ilocloc[0:2, 5:8] )




Q19. What is the output of the following code?

d = {'col1': [1, 4, 3 ], 'col2': [6, 7, 8], 'col3': [9, 0, 1]}
df = pd. DataFrame(d)
print ("Original DataFrame")
print(df)
print("New DataFrame :")
dfn = df.drop(df.index[[1,2]])
print(dfn)




Q20. What is the output of the following code?

data = {'age': [20, 23, 22), 'name': [ 'Ruhi', 'Ali', 'Sam']}
dfi = pd.DataFrame (data, index=[1, 2, 3])
print("Before")
print (df1)
df1['Edu'] = ['BA', 'BE', 'MBA']
print("After")
print(df1)




Q21. Find the error in the following code? Suggest the solution.

>>> topDf
      Rollno  Name  Marks
Sec A  115  Pavni  97.5
Sec B  236  Rishi    98.0
Sec C  307  Preet  98.5
Sec D  422  Paula  98.0
>>> topDf.del['Sec D']




Q22. Find the error in the following code considering the same dataframe topDf given in the previous question.

(i) topdf.rename(index=['a', 'b', 'c', 'd'])
(ii) topdf.rename(columns = {})

18 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. Excuse me ! Where are the solutions

    ReplyDelete
  2. the new edition of the textbook for the year 2021-2022 has not been given here. update your site please.

    ReplyDelete
    Replies
    1. In the new version of book of session 2022-23 , there are 25 questions but in your website there are only 22 que so please update your site

      Delete
  3. There are meant to be 27 questions in type B as per the fourteenth edition (updated in 2023) of book, so could you please update this website?

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post