Chapter 1 : Python Pandas – I
Sumita Arora Book Exercise Solution of Class 12 Informatics Practices [065]
Type B – Short Questions Answers
1. Consider 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 (b) S > 0 (c) S1 = pd.Series(S) (d) S3 = pd.Series(S1)+3
What will be the value of Series object S1 and S3 created above?
Ans : (a) S * 100
0 43.0271
1 61.7328
2 -26.5421
3 -83.6113
(b) S > 0
0 True
1 True
2 False
3 False
(c) S1 = pd.Series(S)
>> Create a same series
0 0.430271
1 0.617328
2 -0.265421
3 -0.836113
(d) S3 = pd.Series(S1) + 3
It will create a new series with value s1+3
0 3.430271
1 3.617328
2 2.734579
3 2.163887
2. 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)
Ans: Output produced
print(S)
AMZN 0.430271
AAPL 0.617328
MSFT -0.265421
GOOG -0.836113
print(S[‘AMZN’] => 0.430271
print(S[‘AMZN’] => 1.5
print(S)
AMZN 1.5
AAPL 0.617328
MSFT -0.265421
GOOG -0.836113
3. What will be the output produced by following code?
Stationary = [‘pencils’, ‘notebooks’, ‘scales’, ‘erasers’]
S = pd.Series([20, 33, 52, 10], index = Stationary)
S2 = pd.Series([17, 13, 31, 32], index=Stationary)
print(S + S2)
S = S + S2
print(S + S2)
Ans:
Stationary = [‘pencils’, ‘notebooks’, ‘scales’, ‘erasers’]
S = pd.Series([20, 33, 52, 10], index = Stationary)
S2 = pd.Series([17, 13, 31, 32], index=Stationary)
print(S + S2)
=>Output-1
pencils 37
notebooks 46
scales 83
erasers 42
S = S + S2
print(S + S2)
=>Output-2
pencils 54
notebooks 59
scales 114
erasers 74
4. 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)
(f) print(S.values)
Ans : Output
(a) Series([], dtype: float64) =>Empty Series
(b) 0 0.430271
(c) 0 0.430271
1 0.617328
(d) 0 12
1 12
2 -0.265421
3 -0.836113
(e) RangeIndex(start=0, stop=4, step=1)
(f) array([12, 12, -0.265421, -0.836113], dtype=float64)
5. 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] = 250
print(S2)
(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) )
Ans:
(a) S2.index => invalid number of indices are given.
S2[5] => index error
(b) S = pd.Series([2,3,4,5], index=range(4))
=> Data must be in the form of sequence
(c) S1 = pd.Series([1,2,3,4], index = range(4))
=> Data must be a sequence,
=> range(n) , where n must be equal to number of data
(d) NO ERROR
6. 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?
Ans: data = np.array([‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’ ] ) => missing of ‘)’
print(s[102, 103, 104])
=> KeyError, writimpoie indices inside the double square brackets
Correct Statemet: print(s[[102, 103, 104]])
7. Why does following code cause error?
S1 = pd.Series(range(1,15, 3), index = list(‘abcd’))
Ans :
S1 = pd.Series(range(1,15, 3), index = list(‘abcd’))
Mismatch of number of values and indices, values are 5 but indices are 4 only.
8. Why does following code cause error?
S1 = pd.Series(range(1,15, 3), index = list(‘abcde’))
print( S1[ ‘ab’ ] )
Ans:
print( S1[ ‘ab’ ] )
KeyError -> ‘ab’ index is not available
9. 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))
Ans:
(a) print(Ser.head( ))
(b) print( Ser.head(8) )
(c) print( Ser.tail( ) )
(d) print( Ser.tail(11))
Similarities => head( ) print top rows, while tail() print bottom rows
Difference=> haed( ) and tail() print only 5 top or bottom rows respectively head(n) and tail(n) prints ‘n’ top rows or bottom rows respectively
10. What advantages does DataFrame offer over Series data structure? If you have similar data stored in multiple series and a single dataframe, which one would you prefer and why?
Ans: Advantages of DataFrame over the Series are –
- DataFrame is a two dimensional data structure while Series is a one dimensional data structure.
- DataFrame allows to store the information just like MySQL table i.e. collection of rows and columns, while Series store one tuple (row/record) information.
- DataFrame is Value as well as Size Mutable while Series is only value Mutable.
- DataFrame allows to store heterogeneous data while Series allows to store homogeneous data.
If we have to store the similar data in Multiple Series and DataFrame, I prefer DataFrame, because
- DataFrame provide multiple functionality in comparison to Series.
- We can easily do export import from DataFrame.
- We can extract individual rows or columns as Series from the DataFrame.
11. 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?
Ans: Output of print(df)
one two
a 1.0 1.0
b 2.0 2.0
c 3.0 3.0
d NaN 4.0
Output of print(df1)
one two
d NaN 4.0
b 2.0 2.0
a 1.0 1.0
Output of print(d2)
two three
d 4.0 NaN
a 1.0 NaN
12. From the dataframes created in previous question, write code to
(a) display only column ‘a’ from dataframes df, dfl, and df2.
(b) display only rows 0 and 1 from dataframes df, dfl, and df2.
(c) display only column ‘a’ and ‘b’ for rows 1 and 2 from dataframes df, dfl and df2.
(d) add an empty column ‘x’ to all dataframes
Answer: (a) display only column ‘a’ from dataframes df, dfl, and df2.
Note : There is no column named ‘a’ in dataframes df, df1 and df2. If we have to access row ‘a’ then
>>> df[‘a’]
>>> df1[‘a’]
>>> df2[0:1]
Answer: (b) display only rows 0 and 1 from dataframes df, dfl, and df2.
>>> df[0:2]
>>> df1.iloc[0:2] # df1[0:2]
>>> df2.iloc[0:2] or df2[0:2]
Questions 13 and 14 make use of this dictionary:
my_di = {“name” . [“Jiya” “Tim”, “Rohan”]’ “
“age” : np.array( 15, 20]),
“weight” : (75,123,239),
“height” : [4.5, 5, 6.1],
“siblings” : 1, “gender” : “M” }
13. Predict the output of following code (it uses above given dictionary my_di)
df = pd.DataFrame(my_di)
print(df)
Ans:
name age weight height siblings gender
0 Jiya 10 75 4.5 1 M
1 Tim 15 123 5.0 1 M
2 Rohan 20 239 6.1 1 M
14. 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)
Ans:
name age weight height siblings gender
Jiya Jiya 10 75 4.5 1 M
Tim Tim 15 123 5.0 1 M
Rohan Rohan 20 239 6.1 1 M
Using the df2 created in Question 14, answer the outputs of questions (15 to 19) given below.
15. 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’ ] )
Ans:
Output of : print( df2[ “weight”] )
Jiya 75
Tim 123
Rohan 239
Name: weight, dtype: int64
Output of : print(df2.weight[‘Tim’])
123
16. 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 )
Ans:
df2[“IQ”] = [130, 105, 115]
# Add a New Column “IQ” with different value for each row
df2[“Married”] = False
# Add a New Column “Married” with one value False
print ( df2 )
Output:
name age weight height siblings gender IQ Married
Jiya Jiya 10 75 4.5 1 M 130 False
Tim Tim 15 123 5.0 1 M 105 False
Rohan Rohan 20 239 6.1 1 M 115 False
- 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 )
name age weight height siblings gender IQ Married College
Jiya Jiya 10 75 4.5 1 M 130 False NaN
Tim Tim 15 123 5.0 1 M 105 False NaN
Rohan Rohan 20 239 6.1 1 M 115 False IIT
18. 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[“Jiya”]) #(a)
print(df2.loc[“Jiya”,”IQ”]) #(b)
print(df2.loc[“Jiya”:”Tim”, “IQ”:”College”]) #(c)
print(df2.iloc[0]) #(d)
print(df2.iloc[0,5]) #(e)
print(df2.iloc[0:2, 5:8]) #(f)
Ans:
#(a) print(df2.loc[“Jiya”])
name Jiya
age 10
weight 75
height 4.5
siblings 1
gender M
IQ 130
Married False
College NaN
Name: Jiya, dtype: object
#(b) print(df2.loc[“Jiya”,”IQ”])
130
#(c) print(df2.loc[“Jiya”:”Tim”, “IQ”:”College”])
IQ Married College
Jiya 130 False NaN
Tim 105 False NaN
#(d) print(df2.iloc[0])
name Jiya
age 10
weight 75
height 4.5
siblings 1
gender M
IQ 130
Married False
College NaN
Name: Jiya, dtype: object
#(e) print(df2.iloc[0,5])
M
#(f) print(df2.iloc[0:2, 5:8])
gender IQ Married
Jiya M 130 False
Tim M 105 False
19. 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)
Answer : Output
Original DataFrame
col1 col2 col3
0 1 6 9
1 4 7 0
2 3 8 1
New DataFrame
col1 col2 col3
0 1 6 9
20. What is the output of the following code?
data = {‘age’ : [20, 23, 22], ‘name’ : [‘Ruhi’, ‘All’, ‘Sam’] }
df1 = pd.DataFrame(data, index=[1, 2, 3])
print( ” Before” )
print(df1)
df1[ ‘Edu’] = [ ‘BA’, ‘BE’, ‘MBA’]
print( ‘After’)
print(df1)
Answer: Output
Before
age name
1 20 Ruhi
2 23 All
3 22 Sam
After
age name Edu
1 20 Ruhi BA
2 23 All BE
3 22 Sam MBA
21. Write a program in Python Pandas to create the following DataFrame batsman from a Dictionary:
B_No | Name | Score1 | Score2 |
1 | Sunil Pillai | 90 | 80 |
2 | Gaurav Sharma | 65 | 45 |
3 | Piyush Goel | 70 | 90 |
4 | Kartik Thakur | 80 | 76 |
Perform the following operations on the DataFrame: [CBSE Sample Paper 20-21]
(i) Add both the scores of a batsman and assign to column “Total”
(ii) Display the highest score in both Score1 and Score2 of the DataFrame.
(iii) Display the DataFrame.
Ans : (i) batsman[“Total”] = batsman[“Score1”] + batsman[“Score2”]
Or
batsman.loc[:,”Total”] = batsman[“Score1”] + batsman[“Score2”]
(ii) print(batsman[‘Score1’].max())
print(batsman[‘Score2’].max())
(iii) print(batsman)
22. Consider the following dataframe, and answer the questions given below:
import pandas as pd [CBSE Sample Paper 19-20]
df = pd.DataFrame({ “Quarter1” : [2000, 4000, 5000, 4400, 10000],
“Quarter2” : [5800, 2500, 5400, 3000, 2900],
“Quarter3” : [20000, 1600,,7000, 3600, 8200],
“Quarter4” : [1400, 3700, 1700, 2000, 6000]} )
(i) Write the code to find mean value from above DataFrame df over the index and column axis.
(ii) Use sum() function to find the sum of all the values over the index axis.
Ans: (i) df.mean()
or
df.mean(axis=’index’)
or
df.mean(axis=0)
Output:
Quarter1 5080.0
Quarter2 3920.0
Quarter3 8080.0
Quarter4 2960.0
dtype: float64
df.mean(axis=’columns’) or df.mean(axis = 1)
0 7300.0
1 2950.0
2 4775.0
3 3250.0
4 6775.0
dtype: float64
(ii) df.sum(axis=’index’) or df.sum()
Output:
Quarter1 25400
Quarter2 19600
Quarter3 40400
Quarter4 14800
dtype: int64
23. Write the use of rename(mapper = <dict-like>, axis=1) method for a Pandas Dataframe. Can the mapper and columns parameter be used together in a rename() method? [CBSE D 2020C ]
Ans: rename(mapper=<dict-like>, axis = 1) is use to change the columns labels of DataFrame. For example –
>>> df
Quarter1 Quarter2 Quarter3 Quarter4
0 2000 5800 20000 1400
1 4000 2500 1600 3700
2 5000 5400 7000 1700
3 4400 3000 3600 2000
4 10000 2900 8200 6000
>>> df.rename(mapper={‘Quarter1′:’Qtr1’, ‘Quarter2′:’Qtr2’, ‘Quarter3′:’Qtr3′,’Quarter4′:’Qtr4’}, axis=1)
Quarter1 Quarter2 Quarter3 Quarter4
0 2000 5800 20000 1400
1 4000 2500 1600 3700
2 5000 5400 7000 1700
3 4400 3000 3600 2000
4 10000 2900 8200 6000
No, We can’t use the mapper and columns parameter together in a rename() method. It will raise an error message:
TypeError: Cannot specify both ‘mapper’ and any of ‘index’ or ‘columns’
24. 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’]
Ans: SyntaxError: invalid syntax, del is not an attribute of DataFrame. del is a statement which is use to delete columns not a row. ‘Sec D’ is an index, it is not a column index. The valid Syntax of del is
del topDF[‘ColumnName’]
25. Find the error in the following code considering the same dataframe topDf given in previous
(a) topDf.rename(index=[‘a’, ‘b’, ‘c’, ‘d’])
(b) topDf.rename(columns={})
Ans :
(a) rename() method need a name-dictionary value for index and columns arguments. In this statement value is given as list, that is wrong. We can write it in this way –
topDf.rename(index={‘Sec A’:’a’, ‘Sec B’:’b’, ‘Sec C’:’c’, ‘Sec D’:’d’})
Rollno Name Marks
a 115 Pavni 97.5
b 236 Rishi 98.0
c 307 Preet 98.5
d 422 Paula 98.0
(b) topDf.rename(columns = { }) : There is no error , but logically it is wrong. Inside the { } braces we have to write the name dictionary. This statement just give the DataFrame as output without any change.
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
Class 12 Informatics Practices [065] – Sumita Arora Book Exercise Solution
- Class 12 IP 065 Ch 1 Python Pandas I Type A Very Short Questions Answer
- Class 12 IP 065 Ch 1 Python Pandas 1 Type B Short Questions Answer
- Class 12 IP 065 Ch 1 Python Pandas 1 Type C Long Answer Questions
- Class 12 IP 065 Ch 2 Python Pandas II Type A Very Short Sumita Arora Book Questions Answer
- Class 12 IP 065 Ch 5 MySQL Revision Tour Check Point Questions Answer
- Class 12 IP 065 Ch 5 MySQL Revision Tour Assignment Questions Answer
- Class 12 IP 065 Chapter 9 Introduction to Computer Networks Sumita Arora Solution
- Class 12 IP 065 Chapter 9 Introduction to Computer Networks Assignments Sumita Arora Solution
- Class 12 Informatics Practices Chapter 6 MySQL Functions Check Point Sumita Arora Exercise Solution
- Class 12 Informatics Practices Chapter 6 MySQL Functions Assignments Sumita Arora Exercise Solution
- Class 12 Informatics Practices 065 Ch 3 Plotting with PyPlot Sumita Arora Book Exercise Solution
- Class 12 Informatics Practices 065 Ch 4 Importing Exporting Data Between CSV Files MySQL and Pandas Sumita Arora Book Exercise Solution
Sumita Arora Solution
- Class 12 Informatics Practices (065) Sumita Arora Book Exercise Solution
- CBSE Class 12 Computer Science Sumita Arora Book Exercise Solution
- Class 11 Informatics Practices (065) Sumita Arora Book Exercise Solution