Pandas1 Q&ans
Pandas1 Q&ans
g. Difference between head() and tail() functions in series. Give example. (2)
OR
SECTION A
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
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
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
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.
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 tail(2) 1
4 Pandas/matplot/numpy 1
5 option a and c 1
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)
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