QUESTION AND ANSWER SECTION
1 What will be the output of following code-
import pandas as pd
s1=pd.Series([1,2,2,7,’Sachin’,77.5])
print(s1.head())
print(s1.head(3))
2 In pandas, S is a series with the followingresult:
S=pd.Series([5,10,15,20,25])
The series object is automatically indexed as 0,1,2,3,4. Write a statement to
assign the series as a, b, c, d, e index explicitly.
3 Write the output of the following :
import numpy as num
import pandas as pd
arr=num.array([1,7,21])
S1 = pd.Series(arr)
print(S1)
4 Write the output of the following code :
import pandas as pd
S1 = pd.Series([31, 28, 31, 30, 31], index = ["Jan", "Feb", "Mar", "Apr", "May"])
print(" ")
print(S1[1:3])
print(" ")
print(S1[:5])
print(" ")
print(S1[3:3])
print(" ")
print(S1["Jan":"May"])
5 Differentiate between Pandas Series and NumPy Arrays
6 Complete the code to get the required output :
import as pd
_ = pd.Series([31, 28, 31], index = ["Jan", "Feb", "Mar"] )
print(S1["_ "])
7 Fill in the blank of given code, if the output is 71.
import pandas as pd
S1 = pd.Series([10, 20, 30, 40, 71,50])
print(S1[ _ ])
8 Write a program to modify the value 5000 to 7000 in the following Series “S1”
A 25000
B 12000
C 8000
D 5000
1
9 Write a program to display only those values greater than 200 in the given Series “S1”
0 300
1 100
2 1200
3 1700
Case study questions:
1. Consider the following Data Frame df and answer questions
A B C
DEPT CS PROD MEDICAL
EMPNO 101 102 103
ENAME ABC PQR LMN
SALARY 200000 100000 20000
i. Write code to delete column B
ii. Write the output of the below code
print(df.tail(2))
iii. Write code to delete row salary
iv. Change the value of column A to 100
v.Change the value of DEPT of B to MECH
vi. Display DEPT and SALARY of column A and B
vii. Write code to rename column ‘A’ to ‘D’ which will not effect original
dataframe
viii. Write code to add a column E with values [CS, 104,XYZ, 300000]
ix. Write code to add a row COMM with values [3000,4000,5000]
x. Write code to rename DEPT to DEPARTMENT which will effect the original
dataframe
xi. Write code to display DEPT in A
i. print(df.A[‘DEPT’])
ii. print(df[‘A’,’DEPT’])
iii. print(df.iloc[1:2,1:2])
iv. print(df.iat[3,2])
xii. Write the output of the statement print(len(df))
i. 3
ii. 4
iii. (4,3)
iv. (3,4)
2
2. Consider the following Data Frame df and answer questions
ACC BST ECO IP
S1 90 91 92 93
S2 94 95 96 97
S3 98 99 100 100
S4 91 92 93 94
i. Create a new column total TOT by adding marks
ii. Find the highest marks scored by student s1
iii. Find the lowest marks scored by student s1
iv. Find the highest marks in ACC
v. Find the lowest marks in IP
3. Consider the following Data Frame df and answer questions
delhi mumbai kolkatta chennai
hospitals 200 300 100 50
population 10 20 30 40
schools 250 350 400 200
i. Display details of city delhiand chennai
ii. Display hospitals in delhi
iii. Display shape of dataframe
iv. Change the population in kolkatta as 50
v. Rename the column population as “pop”
3
4. Consider the following Data Frame df and answer questions
i. Display the name of city whose population >=20
range of 12 to 20
ii. Write command to set all vales of df as 0
iii. Display the df with rows in the reverse order
iv. Display the df with only columns in the reverse order
v. Display the df with rows & columns in the reverse order
5. Consider the following Data Frame df and answer questions
Write the ouput of the following
i. print(len(df))
ii. print(df.count())
iii. print(df.count(1))
iv. print(min(df.loc['SALARY']))
v. print(max(df.loc['ENAME'])