11,12, 13, 14operations On DF

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Worksheet 11 Date

Operations on DF
1 Which function is used to find values from a DataFrame D using the index
number?
a) D.loc
b) D.iloc
c) D.index
d) None of these
2 Which attribute of a dataframe is used to convert rowa into columns and
columns into rows in a dataframe?
a) T
b) ndim
c) empty
d) shape
3 A DataFrame has two axes
a) True
b) False
4 . ___________parameter is used to ignore row labels in
concatenation operation.
a) ignore_index=True
b) ignore_index=False
c) ignoreindex=’true’
d) index_ignore=’False
5 Which of the following is/are characteristics of DataFrame?
a) Columns are of different types
b) Can Perform Arithmetic operations
c) Axes are labeled (rows and columns)
d) All of the above
6 Which of the following function is used to create DataFrame?
a) DataFrame( )
b) NewFrame( )
c) CreateDataFrame( )
d) None of the Above
7 In given code dataframe ‘D1’ has _____ rows and _____ columns.
import pandas as pd
S1 = pd.Series([1, 2, 3, 4], index = ['a', 'b','c','d'])
S2 = pd.Series([11, 22, 33, 44], index = ['a', 'bb','c','dd'])
D1 = pd.DataFrame([S1,S2])
a) 2, 4
b) 4, 6
c) 4, 4
d) 2, 6
8 Assertion (A):
Nidhi has create dataframe Df1

She can expand or delete any row /column in this dataframe.


Reason(R):
In python DataFrame objects can be concatenated or merged
a) Both A and R are true and R is the correct explanation of A.
b) Both A and R are true but R is not the correct explanation of A.
c) A is true but R is false.
d) A is false but R is true.
9 Assertion (A):
DataFrame.count() function will display the sum of the values from
the data frame
Reason (R):
axis=0 ,argument is to used to find sum column-wise
a) Both A and R are true and R is the correct explanation of A.
b) A is true but R is false.
c) A is false but R is true.
d) Both A and R are false
Worksheet 12 Date
Operations on DF

1 Nidhi has created dataframe df1 as following , help her to perform


following tasks and write the code to help her to

1.1)Displays the index (row labels) of DataFrame


a) print( df1.index)
b) print(df1.name)
c) print(df1.row)
d) print(df1.index,row=’values)
1.2)Remove the null value rows
a) df1.rowdelete()
b) Df1.del(np.nan)
c) Df1.drop(np.nan)
d) df1.dropna()
1.3)Returns True/False to show if the DataFrame is
empty
a) Print(df1.nan)
b) Print(df1.null)
c) print(df1.empty)
d) print(df1.NULL)
2 Consider the following code and answer questions:
Riyaz is creating an application using pandas library in his program , his code is
mentioned below. Fill in the blanks to help him
import _____ as pd #Statement A
d={‘a’:[1,2],’b’:[2,3]}
d2={‘a’:[4,5],’b’:[6,7]}
df1=pd.DataFrame(d)
df2=pd.________(d2) # Statement B
df3=pd._____([df1,df2]) # Statement C
2.1)Choose the right code from the following for statement A.
a) pandas
b) df
c) data
d) pd
2.2)Choose the right code from the following for the statement B.
a) Dataframe
b) DataFrame
c) Series
d) Dictionary
2.3)Choose the right code from the following for the statement C.
a) df.index
b) df.shape()
c) df.appenddf()
d) df.concat()
3 Mr. Ankit is working in an organisation as data analyst. He
uses Python Pandas and Matplotlib for the same. He got a
dataset of the passengers for the year 2010 to 2012 for
January, March and December. His manager wants certain
information from him, but he is facing some problems. Help
him by answering few questions given below:

Code to create the above data frame:


import pandas as ____________ #Statement 1
data={"Year":[2010,2010,2012,2010,2012],"Month":["Jan","Mar","Jan",
"Dec",
"Dec"] ,"Passengers":[25,50,35,55,65]}
df=pd.____________________(data) #Statement 2
print(df)

Worksheet 13 Date
3. Choose the right code from the following for statement 1.
1 i. pd
ii. df
iii. data
iv. p
3. Choose the right code from the following for the statement 2.
2 i. Dataframe
ii. DataFrame
iii. Series
iv. Dictionary
3. Choose the correct statement/ method for the required output: (5,3)
3 i. df.index
ii. df.shape()
iii. df.shape
iv. df.size
3. He wants to print the details of "January" month along with the
4 number of passengers, Identify the correct statement:

i. df.loc[['Month','Passengers']][df['Month']=='Jan']
ii. df[['Month','Passengers']][df['Month']=='Jan']
iii. df.iloc[['Month','Passengers']][df['Month']=='Jan']
iv. df(['Month','Passengers']][df['Month']=='Jan')
3. Mr. Ankit wants to change the index of the Data Frame and the
5 output for the same is given below. Identify the correct statement to
change the index.

i. df.index[]=["Air India","Indigo","Spicejet","Jet","Emirates"]
ii. df.index["Air India","Indigo","Spicejet","Jet","Emirates"]
iii. df.index=["Air India","Indigo","Spicejet","Jet","Emirates"]
iv. df.index()=["Air India","Indigo","Spicejet","Jet","Emirates"]
3. Consider the folloowing dataframe and do as directed:
6 import pandas as pd
d={‘Mouse’:[150,200,300,400],
‘Keyboard’:[180,200,190,300],
‘Scanner’:[200,280,330,450]}
df=pd.DataFrame(d,index=[‘Jan’,’Feb’,’March’,’April’])

a)Write code to access data of Mouse and Scanner columns.

b)Write code to access data of scanners using loc[].

c) Write code to access data of rows of jan and march for


scanner and keyboard.

3. What will be the output of following code:


7 dt=({'Name':['Akshit','Bharat','Chetan','Dhaval','Gaurang'],
'InternalMarks':[18,19,20,18,19],
'AnnualExam':[76,78,80,76,73]})
df=pd.DataFrame(dt)
print(df.iloc[0:2,0:2])
Worksheet 14 Date
1 Observe the following dataframe code:
dt=({'Name':['Akshit','Bharat','Chetan','Dhaval','Gaurang'],
'InternalMarks':[18,19,20,18,19],
'AnnualExam':[76,78,80,76,73]})
df=pd.DataFrame(dt)

Which of the following code will print names and Annual marks of
students?
a) print(df.loc[:,’Name’:’AnnualExam’])
b) print(df.loc[‘Name’:’AnnualExam’])
c) print(df.loc[:,df.columns!=’InternalMarks’])
d) All of these
2 Ms. Kavitha wants to print a single column from the dataframe, which
of the following is correct syntax for her?
a) df(col)
b) df<col>
c) df[col]
d) df{df:col}
3 Which of the following is the correct syntax to select or access
columns from the dataframe using column names?
a) df(col1,col2,…,coln)
b) df[[col1,col2,…,coln]]
c) df[col1,col2,…,coln]
d) df{col1:col2:…,:coln}
4 df.index properties can be used to
i. rename rows
ii. rename columns
iii. rename rows and columns both
iv. None of these
5

5.1 Add a column percent with data:[55,56,59,90,56,48]


5.2 Delete the name and marks columns
6 The python code written below has
syntactical errors. Rewrite the correct code
and underline the corrections made.
Import pandas as pd
df ={"Technology":
["Programming","Robotics","3D
Printing"],"Time(in months)":[4,4,3]}
df= Pd.dataframe(df)
Print(df)
7

You might also like