0% found this document useful (0 votes)
40 views14 pages

Pandas1 Q&ans

The document consists of a series of questions and answers related to the Pandas library in Python, covering topics such as creating Series and DataFrames, performing operations, and understanding functions like head(), tail(), loc(), and iloc(). It includes coding exercises, explanations of concepts, and multiple-choice questions. The content is structured into sections with specific tasks and examples to illustrate the use of Pandas for data manipulation and analysis.
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)
40 views14 pages

Pandas1 Q&ans

The document consists of a series of questions and answers related to the Pandas library in Python, covering topics such as creating Series and DataFrames, performing operations, and understanding functions like head(), tail(), loc(), and iloc(). It includes coding exercises, explanations of concepts, and multiple-choice questions. The content is structured into sections with specific tasks and examples to illustrate the use of Pandas for data manipulation and analysis.
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/ 14

SECTION – A

1. a. Which command is used for installing Pandas? (1)


b. Write a suitable Python code to create an empty Series. (1)
c. Find the output of the following code: (1)
import pandas as pd
s1=pd.Series([10,20,30,40,50],index=["a","b","c","d","e"])
print(s1[-4:-2])
d. Write a python panda code to create a 1D array of size 5 with all elements as
zero. Assign 25 to 2nd element. (2)
e. Differentiate between series data structure and dataframe data structure? (2)
f. What is pandas and what is the benefits of pandas? (2)

g. Difference between head() and tail() functions in series. Give example. (2)

h. Write a program to create a pandas series from a dictionary of values and an


ndarray. (4)

OR

h. Write a program to do mathematical operations on series (4 operations). (4)

SECTION A

1. a. pip install pandas 1

1.b. import pandas as pd 1


df=pd.Series()
print(df)
1.c. b 20 1
c 30
dtype: int64
1. d. import pandas as pd 2
s=pd.Series(0,index=[0,1,2,3,4])
s[1]=25
print(s)

1. e. A series is a one-dimensional object that can hold any data type such 2
as integers, floats and strings. It has only one axis.
A dataframe is a two-dimensional object that can hold different data
types. Individual columns of a dataframe can act as a separate series
object.
1. f. Valid answer 2

1.g. Valid answer 2

1.h. import pandas as pd 4


import numpy as np
dict1={"a": (1,3,5,7,9), "b": (2,4,6,8,10)}
ser=pd.Series(dict1)
print(ser)
l1=[1,3,5,7,9]
arr1=np.array(l1)
ser1=pd.Series(arr1)
print(ser1)
OR
import pandas as pd
list1=[11,12,13,14]
s1=pd.Series(list1)
list2=[21,22,23,24]
s2=pd.Series(list2)
print(s1+s2)
print(s2-s1)
print(s1*s2)
print(s1/s2)
SECTION -B

2. a. ................. function saves the data of dataframe on a CSV file. (1)


b. Difference between iterrows() and iteritems() functions in Dataframe. Give
example. (2)
OR
b. Difference between loc() and iloc() functions in Dataframe. Give example. (2)
c. Give the output of the following code: (2)
import pandas as pd
dict={'Name':pd.Series(['Anoop','Abhi','Raju','Mitu']),'Age':pd.Series([16,15
,17,18]), 'Score':pd.Series([57,97,76,65])}
df=pd.DataFrame(dict)
print("Dataframe contents")
print("*********************")
print(df)
d. Write a program that reads and display the productname, stock and price from
“Inventory.csv” file which contains 5 columns named productid,
productname, stock, price and expirydate. (2)
e. Explain Boolean indexing in data frame. Illustrate Boolean indexing using a
data frame program. (2)
f. Write a program to create and perform following operations on rows and
columns of data frame. (6)
(i) creating new row in existing dataframe
(ii) Creating new column in existing dataframe
(iii) print first 3 rows
(iv) print first and third column
(v) delete a column using drop function
(vi) delete a row.
OR
f. Write a program to iterate over a dataframe containing names and marks, which
then calculates grades as per marks (as per guidelines below) and adds them to the
grade column. (6)
Marks >= 90 grade A+;
Marks 70-90 grade A;
Marks 60-70 grade B;
Marks 50-60 grade C;
Marks 40-50 grade D;
Marks <40 grade F;

SECTION B

2. to_csv() 1
a.
2. Valid points 2
b. OR
valid points
2. Dataframe contents 2
c. *********************
Name Age Score
0 Anoop 16 57
1 Abhi 15 97
2 Raju 17 76
3 Mitu 18 65
2. import pandas as pd 2
df=pd.read_csv("<filepath>\\Employee.csv",usecols=['productname','stock','price'])
d. print("Stock Details")
print(df)
2.e. Correct explanation and program 2

2.f. Correct program 6


OR
Correct Program

1. In a DataFrame, axis= 0 ,represents the_____________ elements. (1)


a. Row b. Plot c. Column d. Graph
10. We can delete an element from a series using (1)
a. empty() b. reindex() c. rsub() d. drop()
15. Name the two important data structure of Pandas library. (1)
Series and Dataframe

16. Write the Python command to display the last 4 records of the dataframe df (1)
df.tail(4)
18. Write the command to find the sum of series S1 and S2 (1)
print(S1+S2)

23. DataFrame is created here. Write the answer for any four questions from (i)-
(v) mentioned below.
import pandas as pd
student = {'name': ['Jerin', 'Nelson', 'Mohammed', 'Rafi', 'Rahul', 'Martin', 'Manish'],
'city': ['Mexico City', 'Toronto', 'Prague', 'Shanghai','Manchester','Cairo', 'Osaka'],
'age': [41, 28, 33, 34, 38, 31, 37],'mark': [88.0, 79.0, 81.0, 80.0, 68.0, 61.0, 84.0]}
row_labels = [1, 2, 3, 4, 5, 6, 7]
df = pd.DataFrame(data=student, index=row_labels)
print(df)
(i) To display the name of all the students.
(1)
(I) print(df['city']) (II) print(df.name)
(III)print(df.iloc[:,1]) (IV) print(df.iloc[:,0])
Choose the correct answer from below:
a. Both (I) and (II) b. (II),(III),and (IV)
c. Only (IV) d. Both (II) and (IV)
(ii) To display the city, age and mark of all the students
(1)
(I)print(df('city','age','mark')) (II)print(df.iloc[1:4])
(III)print(df[['city','age','mark']]) (IV)print(df.iloc[:,1:4])
Choose the correct answer from below:
a. Both (I) and (II) b. Only (III) c. Both(III) and (IV) d.
Only (IV)
(iii) Display the details of the student named ‘Mohammed’
(1)
(I) print(df.loc[2,1]) (II) print(df.loc[2])
(III)print(df.loc[2,:]) (IV) print(df.iloc[2,:])
Choose the correct answer from below:
a. (II) , (III) and (IV) b. Only (II) c. Both(III) and (IV) d.
Only (IV)
(iv) Display the details of the students 4 to 7
(1)
(I) print(df.loc[4:7]) (II) print(df.iloc[3:])
(III) print(df.iloc[4:7]) (IV) print(df.loc[3:])
Choose the correct answer from below:
a. Only (III) b. Both (I) and (II) c. Both(III) and (IV) d.
Only (IV)
(v) Display the city in which Rahul lives.
(1)
(I) print(df.city[5]) (II) print(df.iloc['Rahul'])
(III)print(df.iloc[4,1:2]) (IV) print(df.city['Rahul'])
Choose the correct answer from below:
a. (I),(III),(IV) b. Both (I) and (III)
c. Both(II) and (III) d. All of the above
Answer of 23rd Question
23.(i) d. Both (II) and (IV) 1
(ii) c. Both(III) and (IV) 1
(iii) d. Only (IV) 1
(iv) b. Both (I) and (II) 1
(v) b. Both (I) and (III) 1

30. Consider the following DataFrame stud (2)


Admno Name Class
S1 101 Ali X
S2 110 Fadil IX
Write commands to :
i. Add a new column ‘mark’ to the Dataframe stud with values (30,45)
ii. Add a new row with row index S3 and values ( 105 , Murali ,X)
30. i. stud[‘mark’]=[30,45] ii. stud.loc[‘S3’]=[105,’Murali’, ‘X’] 2

32. Differentiate between series data structure and dataframe data structure? (2)
35. 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. (3)
a. print (x*2) b. print(y*2)
Justify your answer.

35. a. will give the output as: [20,40,90,110,20,40,90,110] 3


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.
40. Write a program to create a dataframe ‘product’ containing product_ID,
product_name, qty and price with 5 rows given below.

product_id product_name price qty


0 101 Mobile 1000 8
1 102 AC 2000 5
2 103 Fan 100 20
3 104 TV 3000 3
4 105 Laptop 2500 1
After that create a new column named ‘total_amount’ which calculates
(total_amount=qty*price), then create a new column named ‘Discount’ with
values as
per guidelines given below.
(5)
Condition Discount
total_amount>=10000 10% of total_amount
total_amount>=5000 and total_amount<10000 5% of total_amount
total_amount<5000 2% of total_amount

40 import pandas as pd 5
product=({"product_id":'101','product_name':'Mobile','price':1000,'qty':8},
{"product_id":'102','product_name':'AC','price':2000,'qty':5},
{"product_id":'103','product_name':'Fan','price':100,'qty':20},
{"product_id":'104','product_name':'TV','price':3000,'qty':3},
{"product_id":'105','product_name':'Laptop','price':2500,'qty':1})
df=pd.DataFrame(product)
print(df)
df['total_amount']=df['price']*df['qty']
print(df)
for (rowindex,values) in df.iterrows():
if(values["total_amount"]>=10000):
df.loc[rowindex,"Discount"]=df.loc[rowindex,'total_amount']*.10
elif(values["total_amount"]>=5000 and
values["total_amount"]<10000):
df.loc[rowindex,"Discount"]=df.loc[rowindex,'total_amount']*.05
else:
df.loc[rowindex,"Discount"]=df.loc[rowindex,'total_amount']*.02
print("After Discount Calculation")
print(df)
[2 marks for dataframe creation , 1 mark for total_amount column adding,
2 marks for discount calculation and adding]

3. The command used to display the last 2 rows in a dataframe


is………… (1)
4. Name any one python library generally used for data analysis. (1)
5.Which statement create an empty data frame. (1)
a)>>> s=pd.DataFrame([ ])
b)>>> s=pd.DataFrame(0)
c)>>> s=pd.DataFrame()
d)>>> s=pd.DataFrame([np.NaN])
Choose the correct option from the following
i) option a ii) option b iii) option a and c iv) option d

3 tail(2) 1

4 Pandas/matplot/numpy 1

5 option a and c 1

14.Consider a series object Salary that stores


salary of 4 employee as shown below
Arun 5600 (1)
Rajesh 9200
Rinu 8900
Anil 4500
Write python statement to sort the series in the descending order of salary.
14 Salry.sort_values(ascending=False) 1

19. What is the purpose of iterrows() in pandas? (1)


19 It helps us to loop through each row of a dataframe. It returns an iterator 1
containing index of each row and the data in each row as a series

22. Consider the following DataFrame df and answer any four questions from
(i) to (v)

i) Write down the command to add a new column „Height‟ with values
156,173,140,146,185 (1)
a) df ['Height']=[ 156,173,140,146,185]
b) df. Height=[ 156,173,140,146,185]
c) df (Height) =[ 156,173,140,146,185]
d) both (a) and (b)
ii) Which of the following statement/s will give the exact number of values in
each column of the dataframe? (1)
(i) print(df.count())
(iii) print(df.count) (ii) print(df.count(0))
Choose the correct option. (iv)print(df.count(axis=‟index‟))
a) Both (i) and (ii)
b) Only (i)

c) Both (ii) and (iii) d) (i), (ii) and (iv)


iii) Write down the command to display the column
„Name‟ from the dataframe.
(1)
a) print(df.Name) b) print(df[column]=‟Name‟)
c) print(df[„Name‟]) d) Both (b) and (c)
iv) Write command to display the number of rows and columns in dataframe.
a) print(df.size) b) print(df[index,column]) (1)
c) print(df.shape) d) print(df.ndim)
v) Write command to delete the column “Age‟ from the dataframe. (1)
a) del df[‘Age’] b) drop df['Age']
c) df.del[“Age‟] d) drop[“Age‟]
22 i) df ['Height']=[ 156,173,140,146,185] 4
ii) Option d) (i), (ii) and (iv)
iii) A) OR C) both answers correct
iv) print(df.shape)
v) del df['Age’]

24.Create a series Month (from Jan-May) , from a dictionary having number of


days as data and month name as keys. (2)

24 import pandas as pd 2
dic={'Jan':31,'Feb':28,'Mar':31,'Apr':30,'May':31}
s=pd.Series(dic)
print(s)
a) Replace the index with student name as [Siya, Ram, Fiza, Diya, Manish].
b) Display the failed students (passing mark is 33)
27 i) S.index=['Anoop','Rayan','Meena','Diya','Mahesh'] 1
ii) print(S[S<33])
1

38.The average marks of 5 subjects in three divisions given below: (5)

i) Write a python pandas program to create a dataframe using above data.


ii) Rename the column DIVISION C by DIVISION D.
iii) To display the marks in DIVISION A from dataframe
iv) To display the subjects‟ name from the dataframe
38 i) import pandas as pd 2
dic={'Division A':{'English':65,'Maths':45,'Science':87},
'Division B':{'English':67,'Maths':34,'Science':87},
'Division C':{'English':87,'Maths':87,'Science':56}}
d=pd.DataFrame(dic)
print(d)
ii) print(d.rename(columns={'Division C':'Division D'})) 1
iii)print(d['Division A']) 1
iv) print(d.index.values) 1

You might also like