Pyq Solution
Pyq Solution
output (2)
WOW
1 WOW
2 good
good
great (2)
5 qreat
dtype: object
output
[[0, 1, 2], [0, 1, 2], (0, 1, 2]]
175 R
150
125
l00
0 75
0 50
025
O 00
Create a dataframe with four rows and threc columns and populate it with (3)
random values. Index of the rows arc "Utah', 'Ohio', "Texas', 'Oregon' and
column indexes are 'b','d''e'. Write a lambda function to compute the
column.
difference betwecn the maximum and minimum of cach
Answer
import numpy as np
3),columns=list('bde'),index-['Utah',
frame = pd.DataFrame(np.random.randn4,
"Ohio', 'Texas', 'Oregon'])
Answer
class my dialect(csv.Dialect):
lineterminator -n'
delimiter =:"
quotechar
quoting = csv.QUOTE MINIMAL
reader = cSv.reader(f, dialectmy dialect)
Consider following piece of code and give the output. (3)
import pandas as pd
9, 10],
a = pd. DataFrame ({'id': [1, 2,
'val': ('a', 'b', 'c', 'd']})
12,13, 71,
b = pd. DataFrame ({" id': [1, 7, 10,
'val': 'p', 'g' 'r', 's', 't', 'u'})
pd.merge (a, b, on='id', how='right')
c =
Answer
b.drop_ duplicates( keep-last')
index will be Tuesday of (3)
Generate DateTimelndex of length 20 where each
the third week of a month starting from 10-Jan-2022.
Answer
import pandas as pd
dates-pd.date_ range(2020-01-10', periods-20, freq-"WOM-3TUE')
dates
3 wo -0.283880 -1261686
V. for(kl, k2),group in
df.groupby ([' keyl', 'key2']):
print ((kl, k2) )
print (group)
('a', 'one')
key1 key2 datal data2
one 2.051693 -2.432268
4 one -1. 771815 -1.581653
('a', 'two')
key1 key2 datal data2
a two 0.196488 -0.134805
('b', 'one')
key1 key2 datal data2
2 b one 1.690703 -1.340778
('b', 'two')
key1 key2 data1 data2
b two -0.28388 -1.261686
b) Give output of the following code. Justify.
i. val=[ foo' , 2, [4,2]]
val [2]=(5,4) (2)
print (val)
output
["foo', 2, (5, 4) ]
(2)
ii. var=(3, 5, (4,5) )
var [l]=' two
print (var)
Answer
df- pd.read csv('examples/exI.csv')
df.fillna(0)
df.drop_ duplicates()
df.to csv('examples/ex1 .csv')
Q4 a) What is the use of generator function? Write a generator function to print (4)
square of first n natural numbers where n is user input.
Answer
def square of sequence(x):
for iin range(x):
yield ii
1 one 5
1 foo one
2 one 4
2 foo two
3 one 6
bar one
3 two 7
5 bar one
key2,
ii. prop cumsum -left. sort values (by='
ascending=False).lval.cumsum ()
(2)
3
2 6
Name: lval, dtype: int64
(2)
ii. left.append (right)
key1 key2 lval rval
import pandas as pd
Em = pd.DataFrame( {'Em ID': (1,2,3,4,5],
'Em Name':
[Satesh','Vani''Ramesh,'Rajesh',"Virat'],
'Salary': [5000,7500,10000,8000,9500})
Em
Em_ID Em_Name Salary
Satesh 5000
2 Vani 7500
2 3 Ramesh 10000
3 4 Rajesh 8000
4 Virat 9500
Em.iloc[3:5,2:4]
ii. Print elements of all the columns for first two rows. ()
Em.iloc[:2,]
07 a) Consider the code given below:
import pandas as pd
from datetime import datetie
dates =|datetime (2011,1,2) , datetime (2011,1,5),
dateti me (2011, 1,7) , datetime (2011,1,8),
datetime (201l, 1, 10) , datet ime (2011,1, 12)]
ts = pd. Series (np. random. randn (6), index-dates)