IP Practical File Q & A 2024-25
IP Practical File Q & A 2024-25
Answer :
import pandas as pd
Dict1 = {'India': 'NewDelhi', 'UK':'London', 'Japan': 'Tokyo'}
Sr = pd.Series(Dict1)
print(Sr)
# Output
India NewDelhi
UK London
Japan Tokyo
dtype: object
Answer :
i)
print(Sr['India']) # using labelled index
or
print(Sr[0]) # using positional index
ii)
print(Sr[[2,3]])
or
print(Sr[['UK', 'France']])
or
print(Sr['UK' : 'France']) # using slicing
or
print(Sr[2 : 4]) # using slicing
or
print(Sr.tail(2))
iii)
print(Sr[ : : -1])
Answer :
i) print(Sr[[0,1]])
or
print(Sr[['India', 'USA']])
or
print(Sr['India' : 'USA']) # using slicing
or
print(Sr[0 : 2]) # using slicing
or
print(Sr.head(2))
ii)
Sr.index = [10,20,30,40]
Answer :
# 1
India NewDelhi
USA WashingtonDC
dtype: object
# 2
India NewDelhi
dtype: object
Justification :
Slicing : [startIndex : endIndex : step]
If labelled indexes are used for slicing, then value at the “endindex”
label is also included in the output.
When we use positional indices for slicing, the value at the “endindex”
position is excluded.
Answer :
# 1
a 10
b 50
c 50
d 13
e 14
f 15
dtype: int32
# 2
a 10
b 50
c 50
d 50
e 14
f 15
dtype: int32
Justification :
Slicing : [startIndex : endIndex : step]
When we use positional indices for slicing, the value at the “endindex”
position is excluded.
If labelled indexes are used for slicing, then value at the “endindex”
label is also included in the output.
Answer :
# 1
[10, 11, 12, 10, 11, 12]
# 2
0 20
1 22
2 24
Justification :
“vals” is a list (or tuple) object. When we use it with “*”
(replication) operator, it replicates the structure of list (tuple).
“Sr” is a series object. Series supports vectorization it means –
while using arithmetic operator with a series object, it does
manipulation element-wise instead of manipulating structure.
Answer :
# 1
0 False
1 False
2 True
3 True
# 2
0 10
2 12
Answer :
i) print(Sr.index)
ii) print(Sr.values)
iii) print(Sr.shape)
iv) print(Sr.nbytes)
v) print(Sr.ndim)
vi) print(Sr.size)
vii) print(Sr.itemsize)
viii) print(Sr.hasnans)
ix) print(Sr.empty)
x) Sr.name = “NewSr”
print(Sr)
xi) print(Sr.count())
Answer :
(i) Series1 + Series2
index Series1 Series2 Series1 + Series1
a 1 -10 -9.0
b 2 NaN
c 3 -50 -47.0
d 4 NaN
e 5 100 105.0
y 10 NaN
z 20 NaN
a -9.0
b NaN
c -47.0
d NaN
e 105.0
y NaN
z NaN
dtype: float64
[Note : here that the output of addition is NaN if one of the elements
or both elements have no value.]
(ii) Series1.add(Series2, fill_value=0)
index Series1 Series2 Series1 + Series1
a 1 -10 -9.0
b 2 10 12
c 3 -50 -47.0
d 4 10 14
e 5 100 105.0
y 10 10 20
z 10 20 30
a -9.0
b 12.0
c -47.0
d 14.0
e 105.0
Manish Kumar Sharma – IP – Practical File
Manish Kumar Sharma – IP – Practical File
y 20.0
z 30.0
dtype: float64
Jstification :
We can use the series method add() and a parameter fill_value to
replace missing value with a specified value. By calling
series1.add(series2) is equivalent to calling series1 + series2, but
add() allows explicit specification of the fill value for any element
in series1 or series2 that might be missing.
Table of answer (ii), shows the changes in the series elements and
corresponding output after replacing missing values by 0.
Just like addition [function - add()], subtraction [function - sub()],
multiplication [function - mul()] and division [function - div()] can
also be done using corresponding mathematical operators or explicitly
calling of the appropriate method.
Answer :
import pandas as pd
dFrameEmt = pd.DataFrame()
dFrameEmt
Output :
Empty DataFrame
Columns: []
Index: []
Answer :
# (i) - USING DICTIONARY
import pandas as pd
data1 = { 'Rno' : [11, 12, 13],
'Name' : ['Preeti', 'Priyansh', 'Rohit'],
'Score1' : [80, 90, 100],
'Score2' : [99, 98, 100] }
Student1 = pd.DataFrame(data1)
print(Student1)
# (ii) - USING NESTED LIST
data2 = [ [11, 'Manish', 90, 99],
[12, 'Mahesh', 87, 97],
[13, 'Raman', 100, 100] ]
Student2 = pd.DataFrame(data2,
columns=['Rno', 'Name', 'Score1', 'Score2'])
print(Student2)
# (iii) - USING SERIES OBJECTS
import pandas as pd
data3 = { 'Rno' : pd.Series([11, 12, 13]),
'Name' : pd.Series(['Preeti', 'Priyansh', 'Rohit']),
'Score1' : pd.Series([80, 90, 100]),
'Score2' : pd.Series([99, 98, 100])}
Student3 = pd.DataFrame(data3)
print(Student3)
Answer :
(i) add a new column 'Total' with sum of 'Score1' and 'Score2'
Student1['Total'] = Student1['Score1'] + Student1['Score2']
Or
Student1['Total'] = Student1[['Score1','Score2']].sum(axis=1)
Or
Student1['Total'] = Student1.Score1 + Student1.Score2
Or
Student1.loc[ : , 'Total'] = Student1['Score1'] +
Student1['Score2']
(ii) add a new record with appropriate values
Student1.loc[3] = [4, 'Manish', 98, 99]
Or
Student1.loc[3, :] = [5, 'Akshat', 98, 99]
Or
Student1.iloc[3, :] = [5, 'Akshat', 98, 99]
Answer :
(i)
Student1['City'] = [“Jaipur”, “Jodhpur”, “Ajmer”]
Or
Student1.loc[ : , 'City'] = [“Jaipur”, “Jodhpur”, “Ajmer”]
(ii)
Student1['State'] = “Rajasthan”
Or
Student1.loc[ : , 'State'] = “Rajasthan”
(iii)
Student1.loc[ Student1['Name'] == ’Priyansh’, ‘Name’] = ‘Pawan’
Or
Student1.loc[1, ‘Name’] = ‘Pawan’
(iv)
print(Student1)
Or
print(Student1.loc[ : ])
Or
print(Student1.loc[ : , : ]
Answer :
(i)
print(Student1.Arnab)
Or
print(Student1[‘Arnab’])
Or
print(Student1.loc[:, ‘Arnab’])
(ii)
print(Student1[[‘Arnab’, ‘Riya’]])
Or
print(Student1.loc[:, [‘Arnab’, ‘Riya’]])
(iii)
print(Student1.loc[‘Maths’])
Or
print(Student1.loc[‘Maths’, :])
Or
print(Student1.iloc[0])
Or
print(Student1.head(1))
(iv)
print(Student1.loc[[‘Maths’, ‘Hindi’]])
Or
print(Student1.loc[[‘Maths’, ‘Hindi’], :])
Or
print(Student1.iloc[[0, 3]])
(v)
print(Student1.loc[[‘Maths’, ‘Hindi’], [‘Ramit’, ‘Riya’]])
Answer :
(i)
del Student1[‘Riya’]
Or
Student1.pop(‘Riya’)
Or
Student1 = Student1.drop(‘Riya’, axis=1)
(ii)
Student1 = Student1.drop([‘Arnab’, ‘Mallika’], axis=1)
Or
Student1 = Student1.drop([‘Arnab’, ‘Mallika’], axis=’columns’)
(iii)
Student1 = Student1.drop(‘English’)
Or
Student1 = Student1.drop(‘English’, axis=0)
(iv)
Student1 = Student1.drop([‘Maths’, ‘Hindi’])
Or
Student1 = Student1.drop([‘Maths’, ‘Hindi’], axis=0)
Or
Student1 = Student1.drop([‘Maths’, ‘Hindi’], axis=’index’)
Answer :
(i)
Arnab Ramit Samridhi Riya Mallika
Maths 90 92 89 81 94
Science 91 81 91 71 95
English 97 96 88 67 99
(ii)
Arnab Ramit Samridhi Riya Mallika
Maths 90 92 89 81 94
Science 91 81 91 71 95
Justification :
DataFrame.loc[ ] is used for label based indexing of rows / columns
in DataFrames.
Syntax : loc[row_start : row_stop : row_step, col_start, col_end,
col_step]
DataFrame.iloc[ ] is used for position (integer) based indexing of
rows / columns in DataFrames.
Syntax : iloc[row_start : row_stop : row_step, col_start, col_end,
col_step]
When we use “iloc” for slicing, the value at the “row_stop / col_stop”
label / position is excluded.
If labelled “loc” for slicing, then value at the “row_stop / col_stop”
label / position is also included in the output.
Answer :
Arnab Ramit Samridhi Riya Mallika
Maths 90 92 89 81 94
English 97 96 88 67 99
Justification :
In DataFrames, Boolean values like True (1) and False (0) can be
associated with indices. They can also be used to filter the records
using the DataFrmae.loc[] method.
In order to select or omit particular row(s), we can use a Boolean
list specifying ‘True’ for the rows to be shown and ‘False’ for the
ones to be omitted in the output.
Answer :
(i)
Student1 = Student1.rename({“Arnab” : “Aarti”, “Riya” : “Ram”},
axis=’columns’)
Or
Student1 = Student1.rename({“Arnab” : “Aarti”, “Riya” : “Ram”},
axis=1)
Or
Student1 = Student1.rename(columns={“Arnab” : “Aarti”, “Riya” :
“Ram”})
(ii)
Student1 = Student1.rename({“Arnab” : “Aarti”, “Riya” : “Ram”},
axis=’index’)
Or
Student1 = Student1.rename({“Arnab” : “Aarti”, “Riya” : “Ram”},
axis=0)
Or
Student1 = Student1.rename({“Arnab” : “Aarti”, “Riya” : “Ram”})
Or
Student1 = Student1.rename(index={“Arnab” : “Aarti”, “Riya” : “Ram”})
Answer :
(i)
C1 C2
R1 1 2
R2 5 6
R3 9 10
R1 3 4
R2 7 8
R3 11 12
(ii)
C1 C2 C1 C2
R1 1 2 3 4
R2 5 6 7 8
R3 9 10 11 12
(iii)
C1 C2
R1 1 2
R2 5 6
R3 9 10
R1 3 4
R2 7 8
R3 11 12
Answer :
i) print(Df.index)
ii) print(Df.values)
iii) print(Df.shape)
iv) print(Df.columns)
v) print(Df.ndim)
vi) print(Df.size)
vii) print(Df.empty)
viii) print(Df.T)
ix) print(Df.head(3))
x) print(Df.tail(3))
Answer :
(i)
Arnab Ramit Samridhi Riya Mallika
English 97 96 88 67 99
Hindi 97 89 78 60 45
Maths 90 92 89 81 94
Science 91 81 91 71 95
(ii)
Arnab Mallika Ramit Riya Samridhi
Maths 90 94 92 81 89
Science 91 95 81 71 91
English 97 99 96 67 88
Hindi 97 45 89 60 78
(iii)
Arnab Ramit Samridhi Riya Mallika
Science 91 81 91 71 95
Maths 90 92 89 81 94
Hindi 97 89 78 60 45
English 97 96 88 67 99
Answer :
(i)
Arnab Ramit Samridhi Riya Mallika
Science 91 81 91 71 95
Hindi 97 89 78 60 45
Maths 90 92 89 81 94
English 97 96 88 67 99
(ii)
Arnab Ramit Samridhi Riya Mallika
English 97 96 88 67 99
Maths 90 92 89 81 94
Hindi 97 89 78 60 45
Science 91 81 91 71 95
(iii)
Arnab Ramit Samridhi Riya Mallika
Hindi 97 89 78 60 45
English 97 96 88 67 99
Science 91 81 91 71 95
Maths 90 92 89 81 94
Answer :
(i)
Arnab 375
Ramit 358
Samridhi 346
Riya 279
Mallika 333
dtype: int64
(ii)
Maths 446
Science 429
English 447
Hindi 369
dtype: int64
(iii)
Arnab 93.75
Riya 69.75
dtype: float64
Answer :
(i)
Dfs=pd.read_csv("mydata.csv", header= None, names= ["Rollno", "Name",
"Marks"), skiprows=2, nrows = 3)
(ii)
Dfs.to_csv( "newDfs.csv", index = False, header = False, sep = ';')
Answer.
import matplotlib.pyplot as plt
Classes = ['VII', 'VIII', 'IX', 'X']
Students = [40, 45, 35, 44]
plt.bar(Classes, Students)
plt.title('Class-wise Number of Students')
plt.xlabel('Class')
plt.ylabel('Score[50]')
plt.ylim([30,50])
plt.show()
Answer :
import matplotlib.pyplot as plt
val= [5, 25, 45, 20]
x = [1,2,3,4]
plt.barh(x, val, color= 'b')
plt.title('Bar Chart')
plt.ylabel('Roll Number')
plt.xlabel('Scores[50]')
plt.title('Performance Report')
plt.yticks([1,2,3,4])
plt.xticks([0,5,10,15,20,25,30,35,40,45,50])
plt.show()
Answer :
import pandas as pd
import matplotlib.pyplot as plt
V1=[14, 23, 49, 17]
V2=[15, 25, 45, 20]
V3=[16, 22, 47, 19]
X = pd.Series([1, 5, 9, 13])
plt.bar(X, V1, color = 'b')
plt.bar(X-1, V2, color = 'g')
plt.bar(X+1, V3, color = 'r')
plt.xticks([1, 5, 9, 13])
plt.title('Sales Report')
plt.ylabel(‘Sales’)
plt.xlim(-2, 15)
plt.ylim([10, 55])
plt.show()
Answer :
import pandas as pd
import matplotlib.pyplot as plt
Data= [[5, 25, 45, 20], [8, 13, 29, 27], [9, 29, 27, 39]]
X = pd.Series([0,1,2,3])
plt.plot(X, Data[0], color= 'b', label= 'Dayl')
plt.plot(X, Data [1], color = 'g', label = 'Day2' )
plt.plot(X, Data[2], color= 'r', label= 'Day3')
plt.legend( loc = 'upper left' )
plt.title( "Train's Speed Chart")
plt.xlabel('Check Points')
plt.ylabel('Speed')
plt.xticks([0,1,2,3])
plt.show()
Answer :
import pandas as pd
import matplotlib.pyplot as plt
data = {'Weight' : [60,61,63,65,61,60,47,89,52,58,50,47]}
df=pd.DataFrame(data)
df.plot(kind='hist', edgecolor='Green', linewidth=2, linestyle=':',
fill=False, hatch='o')
plt.show()
Answer :
(i) 16
(ii) 0.25
(iii) -8
(iv) -1
(v) -2
(vi) 1
(vii) 6.3
(viii) 6
(ix) 60
(x) 7.5
(xi) 4
(xii) -7.4
(xii) 300
(xiv) 3
Answer :
(i) 11
(ii) MySQL
(iii) NULL
(iv) 3
(v) 0
(vi) informatics
(vii) INFORMATICS
(viii) Inf
(ix) tics
(x) Informatics
(xi) Informatics
(xii) Informatics
(xiii) formatics
(xiv) ormatics
(xv) form
(xvi) ers
(xvii) ute
(xviii) te
(xix) form
Answer :
(i) 2010-02-26
(ii) 2
(iii) 2010
(iv) 21
(v) 21
(vi) 32
(vii) JULY
Answer :
i. select item_name, unitprice, date_purchase from grocer;
ii. select item_name, month(date_purchase) from grocer;
iii. select item_name, year(date_purchase) from grocer;
iv. select item_name, unitprice, date_purchase,
dayname(date_purchase) from grocer;
v. select item_name from groces where dayname(date_purchase) in
('monday', 'tuesday');
vi. select dayname(date_purchase) from grocer where
item_name='rice';
vi. select item_name, round(unitprice) from grocer;
viii. select curdate();
Answer :
i. select lcase(firstname) from charity;
ii. select ucase(firstname) from charity where city='mumbai';
iii. select p_id, left(firstname, 3) from charity;
iv. select concat(firstname, ' ' ,lastname) from charity;
v. select length(address), p_id from charity;
vi. select right(city,2), p_id from charity;
vii. select lastname, firstname fromcharity where mid(firstname,
2,2)='at';
viii. select lastname, instr(lastname, 'a') from charity;
ix. select lastname, firstname from charity where right(firstname,
1)='a';
x. select concat(trim(firstname),trim(lastname)) from charity;
xi. select p_id, lastname, round(contribution) from charity;
xii. select p_id, lastname, truncate(contribution, 0) from charity;
xiii. select lastname, round(contribution/10, 2) from charity;
Answer :
(A) SELECT LASTNAME, FIRSTNAME FROM EMPLOYEE;
(B) SELECT DISTINCT(DEPARTMENT) FROM EMPLOYEE;
(C) SELECT * FROM EMPLOYEE WHERE LASTNAME = ‘LAKSHMI’;
(D) SELECT * FROM EMPLOYEE WHERE LASTNAME = ‘RAJLAXMI’ OR
LASTNAME=’SHARMA’;
(E) SELECT ECODE, FIRSTNAME FROM EMPLOYEE WHERE DEPARTMENT =
‘ACCOUNTS’;
(F) SELECT * FROM EMPLOYEE WHERE FIRSTNAME LIKE ‘S%’;
Answer :
a.
+----------+----------+
| POW(4,3) | POW(3,4) |
+----------+----------+
| 64 | 81 |
+----------+----------+
b.
+-------------------+-----------------+--------------------+
| ROUND(543.5694,2) | ROUND(543.5694) | ROUND(543.5694,-1) |
+-------------------+-----------------+--------------------+
| 543.57 | 544 | 540 |
+-------------------+-----------------+--------------------+
c.
+----------------------+-----------------------+
| TRUNCATE(543.5694,2) | TRUNCATE(543.5694,-1) |
+----------------------+-----------------------+
| 543.56 | 540 |
+----------------------+-----------------------+
d.
+------------------------------+
| LENGTH("Prof. M. L. Sharma") |
+------------------------------+
| 18 |
+------------------------------+
e.
+---------------+
| FULL NAME |
+---------------+
| SHEIKH HAROON |
+---------------+
f.
+-------------------+--------------------+
| LEFT("Unicode",3) | RIGHT("Unicode",4) |
+-------------------+--------------------+
| Uni | code |
+-------------------+--------------------+
Manish Kumar Sharma – IP – Practical File
Manish Kumar Sharma – IP – Practical File
g.
+-----------------------+-----------------------+
| INSTR("UNICODE","CO") | INSTR("UNICODE","CD") |
+-----------------------+-----------------------+
| 4 | 0 |
+-----------------------+-----------------------+
h.
+------------------------+-----------------------+
| MID("Informatics",3,4) | SUBSTR("Practices",3) |
+------------------------+-----------------------+
| form | actices |
+------------------------+-----------------------+
Answer :
(i)
MIN(DISTINCT Qty)
1
(ii)
MAX(Warranty)
4
(iii)
SUM(CostPerItem)
84000
(iv)
AVG(CostPerItem)
28000