Worksheet Dataframes Solved-23
Worksheet Dataframes Solved-23
A B C
DEPT CS PROD MEDICAL
EMPNO 101 102 103
ENAME ABC PQR LMN
SALARY 200000 100000 20000
print(df.tail(2))
iii. Write code to delete row salary
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
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
population 10 20 30 40
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])