Minimum Level Pandas Skill Based Questions
Minimum Level Pandas Skill Based Questions
1) Consider the following DataFrame df and answer any four questions from (i)-(ii)
(i) The teacher needs to know the marks scored by the student with roll number 4.
Ans:
df1=df[df[‘rollno’]==4]
print(df1)
OR
df1=df[df.rollno==4]
print(df1)
(ii) Ms. Sharma, the class teacher wants to add a new column, Grade
with the values, ‘ A’, ‘B’, ‘A’, ‘A’, ‘B’, ‘A’ ,to the DataFrame.
Ans: df [‘Grade’]=[’A’,’B’,’A’,’A’,’B’,’A’]
Ans:
import pandas as pd
M1=pd.Series([45,65,24,89],index=['term1','term2','term3','term4'])
print(M1)
i. Write the command which will display the name of the furniture having rent>250.
ii. Write the command to name the series as Furniture.
Ans:
i. print(S_amt[S_amt>250])
ii. S_amt.name= 'Furniture'
4) Consider two objects x and y. x is a list whereas 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)
Justify your answer.
Ans:
a. will give the output as:
[20,40,90,110,20,40,90,110]
b. will give the output as
0 40
1 80
2 180
3 220
Justification: In the first statement x represents a list so when a list is multiplied by a
number, it is replicated that many number of times.
The second y represents a series. When a series is multiplied by a value, then each
element of the series is multiplied by that number.
5) Write a program in Python Pandas to create the following DataFrame batsman from a
Dictionary:
Ans:
import pandas as pd
d1={'B_NO':[1,2,3,4], 'Name':["Sunil Pillai","Gaurav Sharma","Piyush Goel","Kartik
Thakur"],'Score1':[90,65,70,80], 'Score2':[80,45,95,76]}
df=pd.DataFrame(d1)
print(df)
df['Total'] = df['Score1']+ df['Score2']
print(df[[‘Score1’,’Score2’]])
Ans:
import pandas as pd
data=[[101,'Gurman',98],[102,'Rajveer',95],[103,'Samar' ,96],[104,'Yuvraj',88]]
df=pd.DataFrame(data,columns=['Rno','Name', 'Marks'])
print(df)
Ans:
i. Stock['Special_Price']=[135,150,200,400]
ii. Stock.loc['4']=['The Secret',800]
iii. Stock=Stock.drop('Special_Price',axis=1)
9) Mr. Som, a data analyst has designed the DataFrame df that contains data about
Computer Olympiad with ‘CO1’, ‘CO2’, ‘CO3’, ‘CO4’, ‘CO5’ as indexes shown
below. Answer the following questions:
Ans:
i. (5,4)
ii. School tot_students Topper First_Runner_up
CO3 GPS 20 18 2
CO4 MPS 18 10 8
B. print(df.loc['CO2': 'CO4', 'Topper'])
OR
print(df.Tot_students-df.First_Runnerup)
10) The python code written below has syntactical errors. Rewrite the correct code
and underline the corrections made.
Import pandas as pd
df ={"Technology":["Programming","Robotics","3DPrinting"],"Time(in months)":[4,4,3]}
df= Pd.dataframe(df)
Print(df)
Ans:
import pandas as pd
df ={"Technology":["Programming","Robotics","3DPrinting"],"Time(in months)":[4,4,3]}
df= pd.DataFrame(df)
print(df)
Ans:
12) Complete the given Python code to get the required output as: Rajasthan
import _________ as pd
di = {'Corbett': 'Uttarakhand', 'Sariska': 'Rajasthan', 'Kanha': 'Madhya Pradesh’,
'Gir':'Gujarat'}
NP = ___________. Series( _____ )
print(NP[ ___________ ])
Ans:
import pandas as pd
di = {'Corbett': 'Uttarakhand', 'Sariska':'Rajasthan', 'Kanha': 'Madhya
Pradesh','Gir':'Gujarat'}
NP = pd.Series( di)
print(NP[ 'Sariska'])
Ans:
import pandas as pd
l=[["Divya","HR",95000],["Mamta","Marketing",97000],["Payal","IT",980000],
["Deepak","Sales",79000]]
df=pd.DataFrame(l,columns=["Name","Department","Salary"])
print(df)
Ans:
i. Genre["Num_Copies"]=[300,290,450,760]
ii. Genre.loc[4]=["Folk Tale","FT",600]
iii.Genre=Genre.rename({"Code":"Book_Code"},axis=1)
OR
Genre=Genre.rename({"Code":"Book_Code"},axis="columns")
15) Ekam, a Data Analyst with a multinational brand has designed the DataFrame
df that contains the four quarter’s sales data of different stores as shown below:
Store Qtr1 Qtr2 Qtr3 Qtr4
0 Store1 300 240 450 230
1 Store2 350 340 403 210
2 Store3 250 180 145 160
Answer the following questions:
i. Predict the output of the following python statement:
a. print(df.size)
b. print(df[1:3])
ii. Delete the last row from the DataFrame.
iii. Write Python statement to add a new column Total_Sales which is the addition of all
the 4 quarter sales.
OR
iii. Write Python statement to export the DataFrame to a CSV file named data.csv
stored at D: drive.
Ans:
i. a. 15
b. Store Qtr1 Qtr2 Qtr3 Qtr4
1 Store2 350 340 403 210
2 Store3 250 180 145 160
ii. df=df.drop(2)
OR
df.drop(2,axis=0)
iii.
df["total"]=df["Qtr1"]+df["Qtr2"]+df["Qtr3"]+df["Qtr4"]
OR
df.to_csv(“D:\data.csv”)
16) Shobit needs to create the following two series named ‘Eng’ and ‘Math’. Help
him to create a DataFrame ‘mydata’ from the given series ‘Eng’ and ‘Math’.
Ans:
import pandas as pd
Eng = pd.Series([25,21,23,24,27],index=['Aditi','bhavuk','chirag','deepak','Gaurav'])
Math = pd.Series([9,29,15,24,20],index=['Aditi','bhavuk','chirag','deepak','Gaurav'])
mydata = pd.DataFrame({'Eng': Eng, 'Math': Math})
Ans:
0 2
1 14
18) Carefully observe the following code :
import pandas as pd
product={'prodid':pd.Series([1,2,3,4,5]),'pname':pd.Series(['pen','pencil',
'eraser','color','sharpener']),'qty':pd.Series([2,10,10,30,10]),
'price':pd.Series([300,20,50,40,15])}
stock=pd.DataFrame(product)
print(stock)
Write Python statements for the following :
(i) Display the names of products.
(ii) Rename the column ‘price’ to ‘newprice’ in the DataFrame stock
Ans:
(i) print(stock['pname'])
(ii) stock=stock.rename({'price':'newprice'},axis='columns')
19) Write a program in Python Pandas to create a series “car” from the following
Dictionary :
dic={
"Model":["Samurai","Accord","CR-V","Nexon"],
"Brand":["Suzuki","Honda","Honda","Tata"],
"Make":[1993,1997,1997,2021]}
Ans:
import pandas as pd
dic = {
"Model": ["Samurai", "Accord", "CR-V", "Nexon"],
"Brand": ["Suzuki", "Honda", "Honda", "Tata"],
"Make": [1993, 1997, 1997, 2021]
}
car = pd.Series(dic)
print(car)
Ans:
(i) result['Grade']=['A','B','A','C']
(ii) result.loc[4]=['Arti',92,'A']
(iii) print(result.head(3))
21)
Ans:
(a) (i) employee=employee.drop('Salary', axis=1)
(ii) employee=employee.drop(4)
(b) employee.to_csv('d:\\data.csv')
OR
employee['new_salary'] = employee['salary'] + 5000
print(employee['new_salary'])