0% found this document useful (0 votes)
460 views

Worksheet Dataframes Solved-23

Uploaded by

rihanais12345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
460 views

Worksheet Dataframes Solved-23

Uploaded by

rihanais12345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

WORKSHEET :PANDAS DATAFRAME

CLASS :XII SUBJECT:IP


1. Consider the following Data Frame df and answer questions:

A B C
DEPT CS PROD MEDICAL
EMPNO 101 102 103
ENAME ABC PQR LMN
SALARY 200000 100000 20000

i. Write code to delete column B

ii. Write the output of the below code

print(df.tail(2))
iii. Write code to delete row salary

iv. Change the value of column A to 100


v. .Change the value of DEPT of B to MECH
vi. Display DEPT and SALARY of column A and B
vii. Write code to rename column ‘A’ to ‘D’ which
will not effect original dataframe
viii. Write code to add a column E with values [CS,
104,XYZ, 300000]
ix. Write code to add a row COMM with values
[3000,4000,5000]
x. Write code to rename DEPT to DEPARTMENT which
will effect the original dataframe
xi. Write code to display DEPT in A
i. print(df.A[‘DEPT’])
ii. print(df[‘A’,’DEPT’])
iii. print(df.iloc[1:2,1:2])

iv. print(df.iat[3,2])
xii. Write the output of the statement print(len(df))

i. 3
ii. 4
iii. (4,3)
iv. (3,4)

Answers :=

i. del df['A']

ii. A B C
ENAME ABC PQR LMN
SALARY 200000 100000 20000
iii. df=df.drop(['SALARY'],axis=0)

iv. df['A']=100
v. df.B['DEPT']='MECH'

vi. print(df.loc[['DEPT','SALARY'],["A","B"]])

vii. df.rename(columns={"A":"D"},inplace=False)
viii. df['E']=["CS",104,"XYZ",300000]
ix. df.loc['COMM']=[3000,4000,5000]
x. df.rename(index={"DEPT":"DEPARTMENT"},inplace=True)

xi. print(df.A[‘DEPT’])

xii. 4

2.Consider the following Data Frame df and answer questions


ACC BST ECO IP
S1 90 91 92 93
S2 94 95 96 97
S3 98 99 100 100
S4 91 92 93 94
Create a new column total TOT by adding marks
i. Find the highest marks scored by student s1
ii. Find the lowest marks scored by student s1
iii. Find the highest marks in ACC
iv.
Find the lowest marks in IP

Answers:=
i. df['TOT']=df['ACC']+df['BST']+df['ECO']+df['IP']
ii. print(max(df.loc['S1',:]))
iii. print(min(df.loc['S1',:]))
iv. print(max(df['ACC']))
v. print(min(df['IP']))
3.Consider the following Data Frame df and answer questions

delhi mumbai kolkatta chennai

hospitals 200 300 100 50

population 10 20 30 40

schools 250 350 400 200

i. Display details of city delhi and chennai


ii. Display hospitals in delhi
iii. Display shape of dataframe
iv. Change the population in kolkatta as 50
v. Rename the column population as “pop”
Answers:=
i. print(df[['delhi','chennai']])
ii. print(df.delhi['hospitals'])
iii. print(df.shape)
iv. df.kolkatta['population']=50
V.df.rename(index={"population":"pop"},inplace=True)

4.Consider the following Data Frame df and answer questions

i. Display the name of city whose


population >=20 range of 12 to
20
ii. Write command to set all vales of df as 0
iii. Display the df with rows in the reverse order
iv. Display the df with only columns in the reverse order
v. Display the df with rows & columns in the reverse
order

Answers:-
i. print(df[df.population>=20])
ii. df[:]=0
iii. print(df.iloc[::-1)
iv. print(df.iloc[:,::-1])
v. print(df.iloc[::-1,::-1])
2. Consider the following Data Frame df and & answer questions
Write the ouput of the following
i. print(len(df))
ii. print(df.count())
iii. print(df.count(1))
iv. print(min(df.loc['SALARY']))
v. print(max(df.loc['ENAME']))
Answers:
i. 4
ii. A 4
B 4
C 4
dtype: int64
iii. DEPT 3
EMPNO 3
ENAME 3
SALARY 3
dtype: int64
iv. 20000
v. PQR
QUESTIONS ON DATAFRAME
1. What are the purpose of following
statements- 1.df.columns
2. df.iloc[ : , :-5]
3. df[2:8]
4. df[ :]
5. df.iloc[ : -4 , : ]
Ans:
1. It displays the names of columns of the Dataframe.
2. It will display all columns except the last 5 columns.
3. It displays all columns with row index 2 to 7.
4. It will display entire dataframe with all rows and columns.
5. It will display all rows except the last 4 four rows
2. What will be the output of df.iloc[3:7,3:6]?
Ans:
It will display the rows with index 3 to 6 and columns with index 3 to 5 in
a dataframe ‘df’.
3.Write a python program to create a data frame with headings
(CS and IP) from the list given below-
[[79,92][86,96],[85,91],[80,99]]
Ans:
l=[[10,20],[20,30],[30,40]]
df=pd.DataFrame(l,columns=['CS','
IP'])
print(df)
4.Write python statement to delete the 3rd and 5th rows
from
Dataframe df.

df1=df.drop(index=[2,4],axis=0)

OR

df1=df.drop([2,4])

You might also like