1.
Write
a program to create a series object using a dictionary that stores the number
of students in each house of class 12D of your school.
Ans:
import
pandas as pd
dict={'Teresa':34,'Akbar':43,'Ranjeet':32,'Ashoka':22}
se=pd.Series(dict)
print(se)
2.
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
: 0 False
1
False
2
True
3
False
3.
Carefully
observe the following code
import pandas as pd
Year1={'Q1':5000,'Q2':8000,'Q3':12000,'Q4':18000}
Year2={'A':13000,'B':14000,'C':12000}
totSales={1:Year1,2:Year2}
df=pd.DataFrame(totSales)
print(df)
Answer
the following:
(i)
List
the index of the DataFrame df
(ii)
List
the column names of DataFrame df.
Ans:
print(df.index)
print(df.columns)
4.
To
create a series storing the data and index as follows, the statement will be
Index Values
24 12
27 14
30 16
33 18
Ans:
import pandas as pd
se=pd.Series([12,14,16,18],index=[24,27,30,33])
print(se)
5.
Given
the following code for a series creation, write the output of the following
statement:
S1=pd.Series([1, 2, 3, 4])
print(S1[0:2]*2)
Ans:
0
2
1 4
dtype:
int64
6.
Consider
a given Series, A:
Marks
U1 65
U2 43
U3 60
U4 78
Write
a program in Python Pandas to create a Series.
Ans
: import pandas as pd
A=pd.Series([45,43,60,70],index=['U1','U2','U3','U4'])
print(A)
7.
A
series ‘Subject’ comprises of following data
Index Values
0
Mathematics
1
Physics
2
Chemistry
3
Computer
Science
What will be
output of the following code?
for s in Subject:
if len(s)>9:
print(s*2)
Ans:
Mathematics
Computer
Science
8.
Consider
the following DataFrame, df1
ID Name Salary Subject
F1 101 Shreya 15000 Hindi
F2 102 Kirti 18000 Science
F3 103 Aryan 17000 English
F4 104 Vansh 22000 Computer
Write commands to
(i)
Add
a new column Experience to the DataFrame.
(ii)
Add
a new row values (105, Sahil, 18000, Science).
Ans:
(i)
df1['Experience']=0
(ii)
df1.loc['F5']={'ID':105,
'Name':'Sahil', 'Salary':18000,'Subject': 'Science'}
9.
Series
object S1 stores the amount of product
A 4000
B 5200
C 6500
D 3800
E 4600
Write a program to display which product more
than 6000.
Ans:
import pandas as
pd
amount=[4000,5200,6500,3800,4600]
idx=['A','B','C','D','E']
S1=pd.Series(amount,
idx)
print(S1)
print(S1[S1>6000])
10.
A Series s1 is created as
import pandas as p
s1=p.Series([1,2,3,4])
print(s1.hasnans)
Ans
: False
11.
Consider
the following DataFrame, Class
Enroll Name Score Percentage
A1 1 Rahul 76 78
A2 2 Kiyaan 82 76
A3 3 Karan 78 89
A4 4 Prashant 92 88
Write commands to
(i)
Add
new Column ‘Grade’ to the DataFrame.
(ii)
Add
a new row with values (5, Kirti, 69,72)
Ans:
(i)
Class['Grade']=0
(ii)
Class.loc['F5']={'Enroll':5,
'Name':'Sahil', 'Score':69,'Percentage': 72}
12.
A
Series object ‘Ser’ consists of around 100 rows of data. Write a program to
print the first 50 rows of data.
Ans:
Ser.head(50)
13.
Given
a DataFrame storing marks of some college students as follows:
import pandas as pd
dict={'Name':['Ram','Vedant','Sameer','Siyam'],
'Maths':[78,89,91,56],
'Science':[89,90,91,93]}
df=pd.DataFrame(dict)
df.loc[len(df.index)]=['Arpit',89,93] #L1
print(df)
What will be the output of statement #L1
Ans:
Name Maths Science
0 Ram 78 89
1 Vedant 89 90
2 Sameer 91 91
3 Siyam 56 93
4 Arpit 89 93
#L1Ã Dict (dataFrame) will add a new row after finding the length of the index in the data frame.
14. Consider the
Series ,a
Code Word
A1 Honesty
A2 Loyalty
A3 Charming
A4 Intelligent
A5 Wise
(i)
Write
the command which will display the word having Code A2.
(ii)
Write
the command to name Series as Quanlity.
Ans:
(i)
a.loc[‘A2’]
OR
a.iloc[1]
OR
a.A2
(ii)
S1.name=
‘Quantity’
15. Consider the
following DataFrame df1:
Code Name M1 M2 M3
0 11 Riya 45 39 53
1 12 Shobhit 72 58 49
2 13 Kiyaan 48 62 72
3 14 Vivek 65 70 59
4 15 Aashi 79 81 78
Write the output
of the following statements:
(i)
df.max()
(ii)
print(df.count(0))
Ans
: (i) Name Vivek
M1 79
M2 81
M3 78
(iii)
Code 5
Name 5
M1 5
M2 5
M3 5
dtype: object
16. Find the error and
Rewrite the below code to create a series properly.
import pandas as
p1
Lst1=[1,2,3]
Lst2=list(‘ABC’)
S1=p1.Series(Lst1, rows=Lst2)
Ans:
import pandas as p1
Lst1=[1,2,3]
Lst2=list('ABC')
S1=p1.Series(Lst1, index=Lst2)
17. Given a DataFrame
graded:
import pandas as
pd
gradedf=pd.DataFrame({'Name':['Rashmi','Ani','Sunil','Becker'],
'Grade':['A','B','C','D']})
print(gradedf.iloc[0:2])
Find the output
Ans: Name Grade
0
Rashmi A
1 Ani B
18. Given the Series A
and B.
A |
|
B |
||
0 |
3 |
|
0 |
10 |
1 |
4 |
|
1 |
9 |
2 |
7 |
|
2 |
7 |
3 |
2 |
|
3 |
4 |
Write
the command to find the difference of Series A and B.
Ans: print(A-B)
19. Answer the following based on the Series given
below:
import pandas as
pd
profit=[350,200,800,150]
idx=['TCS','Reliance','L
& T','Wipro']
company=pd.Series(profit,index=idx)
company.name=
‘POCO’
What will be the
output of
(i)
print(company*2)
(ii)
print(company[‘L
& T’])
Ans:
(i)
TCS 700
Reliance 400
L & T
1600
Wipro 300
Name: POCO, dtype: int64
(ii)
800
20. Given a DataFrame
‘vaccine’ store the following:
A |
TokenNo |
Vaccine |
Price |
0 |
T567 |
Covaxin |
780 |
1 |
T667 |
Covishield |
900 |
2 |
T990 |
Sputnik |
1200 |
Write the Python statement to :
(i)
Increase
the price the vaccines by 10%.
(ii)
Price
of Sputnik Vaccine
Ans:
(i)
vaccine.Price=df.Price+df.Price*0.10
OR
vaccine [‘Price’]= vaccine [‘Price’]+ vaccine [‘Price]*0.10
(ii)
df.iat[2,2] OR df.at[2,’Price’]
21. Given a DataFrame
created as:
import pandas as p
Stud=p.Series({'Name':['Ria','Priya','Jack'],'Eco':[56,67,89],'IP':[90,87,45],'Eng':[58,78,32]})
print(Stud)
Studdf=p.DataFrame(Stud)
print(Studdf[0:2])
What will be the output of the
above code?
Ans:
0
Name [Ria, Priya, Jack]
Eco [56, 67, 89]
22. Rama created a
Series storing the names and prices of icereams as follows:
import pandas as p
iceream=pd.Series([150,200,300],index=['Blackberry','Blackforest','Butter'])
Write the python statements for the following:
(i)
She
wants to display the icecream prices increased by 10.
(ii)
The
command that display the third row.
Ans:
(i) print(icecream+30)
(ii) icecream.iloc[2] OR icecream.loc['Butter'] OR icecream.Butter
23. Consider the
following Series animal:
L Lion
B Bear
E Elephant
T Tiger
W Wolf
dtype: object
Write output of
the command:
print(animal[::-3])
Ans:
W Wolf
B Bear
dtype: object
24. Given a code:
Numseries=pd.Series([12,
14, 16])
Write the python
command to:
(i)
Display
the double of each element of Sries.
(ii)
Display
all odd values of Series.
Ans:
(i) print(Numseries*2)
(ii)
print(Numseries[Numseries%2!=0])
25. What will be the
output of the given code:
import pandas as
pd
s=pd.Series([1,2,3,4,5],index=['Manoj','Ravi','Jyoti','Aviral','Anshu'])
print(s['Jyoti'])
Ans:
3
26. Consider the
following DataFrame df:
Name Age Marks
0 Anki 33 67.7
1 Vanshi 36 NaN
2 Jyoti 40 90.0
Write the python
command:
(i)
To
display the half of the marks obtained by all students.
(ii)
To
add new column to the DataFrame with name ‘Percentage’ with relevant values.
Ans:
(i) print(df.Marks/2)
(ii) df['Percentage']=[90,89,78]
27. A Social Science
teacher wants to use a Pandas series to teach about Indian Historical Moinments
and its States. The series should have the Monument names as values and state
names as indexes. Write the Python code to do so.
Ans:
import
pandas as pd
Monument=['Qutub
Minar','Gatway of India','Red Fort','Taj Mahal']
State=['Delhi','Maharastra','Delhi','Uttar
Pradesh']
S=pd.Series(Monument,index=State)
print(S)
28. Consider the
following dataframe df1:
Ename Sal Bonus
0 Kavita 50000 3000
1 Sudha 60000 4000
2 Garima 55000 5000
Write the output
of the given command:
print(df1.loc[:0,'Sal'])
Ans
: 5000
29. Consider the
following Series in Python:
Data=pd.Series([5,2,3,7],index=['a','b','c','d'])
Write the Python
statements to:
(i)
Display
all odd values of the series.
(ii)
Display
the number of elements in the series.
Ans
: (i) print(Data[Data%2!=0])
(ii) Data.size
No comments:
Post a Comment