Shree Krishna International School, Vapi
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. 2Name some common data structures of Python's Pandas library.
Answer =
Some common data structures of Python's Pandas library are:-
DataFrame, Series, panel
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
x = np.array([[1, 2, 3], [4, 5, 6]], np.int32)
type(x)
<class 'numpy.ndarray'>
Q.4 Write a single line
Pandas Statement for the following (Assuming necessary modules have been imported):
Declare a Pandas Series named Packets having dataset as:
[125,92,104,92,85,116,87,90]
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)
Q. 6 Given following Series objects:
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
Q. 8 Given a dataframe df as shown below:
A B C
0 15 17 19
1 16 18 20
2 20 21 22
What will be the result of following code statements?
(a) df['C'] = np.NaN
(b) df['C'] = [2, 5]
(c) df['C'] = [12, 15, 27]
Answer =
import pandas as pd
import numpy as np
dict={'A':[15,16,20],'B':[17,18,21],'D':[19,20,22]}
df=pd.DataFrame(dict)
print(df)
(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. 13 Write statement(s) to delete a row from a DataFrame.
Answer =
df.drop([row index/rowLabel ])
Q. 14 Write statement(s) to delete a column from a DataFrame.
Answer =
del df [ 'Column Name']
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).
Q. 18 What is the difference between iat and at with respect to a DataFrame?
Answer =
iat - position based
Works similarly to iloc. Cannot work in array indexers. Cannot! Assign new indices and
columns
at - label based
Works very similar to loc for scalar indexers. Cannot operate on array indexers. Can!
Assign new indices and columns.
Q. 19 How would you delete rows from a dataframe?
Answer =
By using .drop() function
df.drop(index/Row label )
Q. 20 How would you delete rows from a dataframe?
Answer =
By using .drop() function
df.drop(index/Row label )
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 ”})