CH 1 Type A Exercise
CH 1 Type A Exercise
Plot No. 12, Charwada Road, Gunjan Road, Vapi-396151 Dist.-Valsad, Gujarat.
Std -12
Chapter -1 (Python Pandas -I)
Exercise- Type- A Solutions
Q.1 What is the significance of Pandas library?
Answer =
1- Pandas is an open source.
2- Pandas is BSD library built for python programming language.
3- Pandas offers high performance, easy to use data structures and data analysis tools.
4- It can read or write in, many different data formats.
5- It supports reshaping of data into different forms.
Q. 3 How is a Series object different from and similar to ndarrays? Support your answer with
examples.
Answer =
Series is 1D structure. It is a homogeneous data structure. Mutable type but size is
immutable.
NDarray is an N Dimensional array.
Example:-
A 2-dimensional array of size 2 x 3
Answer
import pandas as pd
list=[125,92,104,92,85,116,87,90]
Packets=pd.Series(list)
print(Packets)
Q. 5Write commands to print following details of a Series object seal.
(a) If the series is empty.
(b) Indexes of the series.
(c) The data type of underlying data.
(d) If the series stores any NaN values.
Answer =
(import pandas as pd
seal=pd.Series()
print(seal) OR print(seal.empty)
print(seal.index)
print(seal.size)
print(seal.hasnans) OR print(seal.count)
S1
A 10
B 40
C 34
D 60
S2
A 80
B 20
C 74
D 90
Write a command to find the sum of series S1+S2
Answer =
import pandas as pd
S1=pd.Series([10,40,34,60])
S2=pd.Series([80,20,74,90])
print(S1)
print(S2)
print(S1+S2)
output
0 90
1 60
2 108
3 150
dtype: int64
Q. 7 Consider two objects x and y. x is a list where as y is a Series. Both have values
20,40,90,110. What will be the output of the following two statements considering that the
above objects have been created already.
a) print(x*2) b) print(y*2)
Answer
import pandas as pd
x=[20,40,90,110]
y=pd.Series([20,40,90,110])
print(x*2)
print(y*2)
output
[20, 40, 90, 110, 20, 40, 90, 110]
0 40
1 80
2 180
3 220
dtype: int64
(a)
A B D C
0 15 17 19 NaN
1 16 18 20 NaN
2 20 21 22 NaN
(b)
ValueError: Length of values (2) does not match length of index (3)
(c)
A B D C
0 15 17 19 12
1 16 18 20 15
2 20 21 22 27
Q. 9 Write code statements to list the following, from a DataFrame namely sales.
(a) List only columns 'Item' and 'Revenue'.
(b) List rows from 3 to 7.
(c) List the value of cell in 5th row, 'Item' column.
Answer =
Q.10 Hitesh wants to display the last four rows of DataFrame df and has written the following
code.
df.tail()
But last 5 rows are being displayed. Identify the error and rewrite the code so that lats 4 rows
will be displayed
Answer It should be
df.tail(4)
Q. 11How would you add a new column namely 'val' to a dataframe df that has 10 rows in it
and has columns as 'Item', 'Qty', ‘Price’? You can choose to put any values of your choice.
Answer =
df[“val”] = [0,10,20,30,40,50,60,70,80,90]
Q. 12Write code statements for a dataframe df for the following:
(a) Delete an existing column from it.
(b) Delete rows from 3 to 6 from it.
(c) Check if the dataframe has any missing values.
(d) Fill all missing values with 999 in it.
Answer =
(a) del df[“existing column name ”]
(b) df.drop([2,3,4,5])
(c) df.isnull()
(d) df.fillna(999)
Q. 15Write statement(s) to change the value at 5th row, 6th column in a DataFrame df.
Answer =
df [ "6th column name"] [5th row index / row label] = New value
Q. 16 Write statement(s) to change the values to 750 at 4th row to 9th row, 7th column in a
DataFrame df.
Answer =
df ["7th column name "] [4:7] = 750
Q. 17 What is the difference between iloc and loc with respect to a DataFrame?
Answer =
• loc gets rows (or columns) with particular labels from the index.
• iloc gets rows (or columns) at particular positions in the index (so it only takes integers).
at - label based
Works very similar to loc for scalar indexers. Cannot operate on array indexers. Can!
Assign new indices and columns.
Q. 21 Which function would you use to rename the index/column names in a dataframe?
Answer =
By using df.rename()
df.rename(index={“old row name”:”new row name ”})