0% found this document useful (0 votes)
174 views8 pages

Minimum Level Pandas Skill Based Questions

Uploaded by

maazmaaz20061013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
174 views8 pages

Minimum Level Pandas Skill Based Questions

Uploaded by

maazmaaz20061013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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’]

2) Consider a given Series , M1:

Write a program in Python Pandas to create the series.

Ans:
import pandas as pd
M1=pd.Series([45,65,24,89],index=['term1','term2','term3','term4'])
print(M1)

3) Consider the following Series object, S_amt

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:

Perform the following operations on the DataFrame :


1)Add both the scores of a batsman and assign to column “Total”
2)Display both Score1 and Score2 of the DataFrame.

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’]])

6) What will be the output of the following code:


>>>import pandas as pd
>>>A=pd.Series(data=[35,45,55,40])
>>>print(A>45)
Ans:

7) Write a Python code to create a DataFrame with appropriate column headings


from the list given below:
[[101,'Gurman',98],[102,'Rajveer',95],[103,'Samar' ,96],[104,'Yuvraj',88]]

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)

8) Consider the given DataFrame ‘Stock’:


Name Price
0 Nancy Drew 150
1 Hardy boys 180
2 Diary of a wimpy kid 225
3 Harry Potter 500
Write suitable Python statements for the following:
i. Add a column called Special_Price with the following data:
[135,150,200,440].
ii. Add a new book named ‘The Secret' having price 800.
iii. Remove the column Special_Price.

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:

A. Predict the output of the following python statement:


i. df.shape
ii. df[2:4]
B. Write Python statement to display the data of Topper column of indexes CO2 to CO4.
OR (Option for part iii only)
Write Python statement to compute and display the difference of data of
Tot_students column and First_Runnerup column of the above given
DataFrame.

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)

11) Predict the output of the given Python code:


import pandas as pd
list1=[-10,-20,-30]
ser = pd.Series(list1*2)
print(ser)

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'])

13) Create a DataFrame in Python from the given list:


[[‘Divya’,’HR’,95000],[‘Mamta’,’Marketing’,97000],[‘Payal’,’IT’,980000],
[‘Deepak’,’Sales’,79000]]
Also give appropriate column headings as shown below:

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)

14) Consider the given DataFrame ‘Genre’:


Type Code
0 Fiction F
1 Non Fiction NF
2 Drama D
3 Poetry P
Write suitable Python statements for the following:
i. Add a column called Num_Copies with the following data:
[300,290,450,760].
ii. Add a new genre of type ‘Folk Tale' having code as “FT” and 600 number of copies.
iii. Rename the column ‘Code’ to ‘Book_Code’.

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})

17) What will be the output of the following code ?


import pandas as pd
S1=pd.Series(data=[1,7])
S2=pd.Series(S1+S1)
print(S2)

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)

20) Consider the given DataFrame :


Name Percentile
0 Rohit 95
1 Mohit 76
2 Raman 98
3 Aditya 47
Write the suitable Python statements for the following :
(i) Add a new column ‘Grade’ to the dataframe having values A,B,A,C
(ii) Add a new row where Name is ‘Arti’ with Percentile as 92 and Grade A.Grade A.
(iii) Display the top 3 rows.

Ans:
(i) result['Grade']=['A','B','A','C']
(ii) result.loc[4]=['Arti',92,'A']
(iii) print(result.head(3))
21)

(a) Write Python statements for the DataFrame ‘employee’:


(i) To remove the column ‘Salary’.
(ii) To remove the row having index 4.
(b) Write Python statement to save the DataFrame ‘employee’ to a CSV file data.csv
stored in D: drive of the computer.
OR
(b) Write a Python statement to display the new salary i.e., salary increased by 5000 for
all employees.

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'])

You might also like