In this article we will discuss QnA – Dataframe functions.
Topics Covered
Objective Type Questions – dataframe functions
Click here to read the notes for the same.
Fill in the blanks:
- A _________function is used to display top rows (n) from dataframe.
- A _______ function is used to display bottom rows (n) from dataframe.
- _____ refers to passing True and False value as an index in Dataframes.
- To pass index label _________ keyword is used to in pd.dataFrame() function.
- By default you can display ____ no. of top/bottom rows using head()/tail() function.
Fill in the blanks Answers for dataframe functions:
- head()
- tail()
- ignore_index
- index
- 5 (five)
Name | City | Fees | ||
0 | Aksh | Ahmedabad | aksh123@gmail.com | 15000 |
1 | Bhavin | Baroda | bhavin000@gmail.com | 25000 |
2 | Charu | Surat | charu123@gmail.com | 12000 |
3 | Dhara | Anand | dhara174@gmail.com | 11000 |
Now in the nest section we will see MCQs from the Dataframe functions topic:
MCQs
1 Choose the correct function to rename city columns to location using rename() function:
a. df.rename(columns={‘City’:’Location’})
b. df.rename(columns={‘City’=’Location’})
c. df.rename(‘City’=’Location’)
d. df.rename(df.columns(‘City’,’Location’))
2 Which of the following statement(s) is/are correct with respect to df.columns properties to rename columns
- All columns must be specified
- Columns must be in the form of a list
- Old column names not required
- Columns can be specified with columns number
a Only 1 is correct
b 1, 2 and 3 are correct
c 1 and 3 are correct
d All of them are correct
3 df.index properties ca be used to
a rename rows
b rename columns
c rename rows and columns both
d None of these
4. To display 2 rows from the top in the dataframe, which of the following statement is correct:
a df.head()=2
b. df.head(n=2)
c. df.head(range(2))
d. All of the above
Error based Questions – Dataframe functions
- Find errors from the given code fragments:
df.DataFrame({'S.NO':[1,2,3],'Name':['Sapan','Vivek','Vishal']})
df.rename[{'S.No':'SNO','Name':'Sname'}]
df.index=(1,2,3)
Errors | Correction |
Line 1: Dataframe Object is missing | df = pd.DataFrame({‘S.NO’:[1,2,3],’Name’:[‘Sapan’,’Vivek’,’Vishal’]}) |
Line 2: columns keyword is missing, and rename is a function so bracket needs to be replaced to correct | df.rename(columns={‘S.No’:’SNO’,’Name’:’Sname’}) |
Lines 3: index properties requires a list not in a tuple form | df.index=[1,2,3] |
2.
df.DataFrame({'S.NO':[1,2,3],'Name':['Sapan','Vivek','Vishal']})
df.columns=('SNO','Name')
df.index=[1 to 3]
3.
df.DataFrame({'S.NO':[1,2,3],'Name':['Sapan','Vivek','Vishal']})
df.head(3)
df.tail(3)
Application Based questions
Watch this video understand the topic and answers.
1 Consider above dataframe and rename columns using rename() function and df.columns properties.
Old column Names : City, Email, Fees
New Column Names: Location, ContactDetals, Charges
Sol.
1 Using Rename Function:
df.rename(columns={'City':'Location','Email':'ContactDetails','Charges'})
2 Using columns properties:
df.columns=['Location','ContactDetails','Charges']
2 Consider above dataframe and rename rows using rename() function and df.index properties.
New indexes: 1,2,3,4
Sol.
1 Using rename function:
df.rename(index={0:1,1:2,2:3,3:4})
2 Using index properties:
df.index=[1,2,3,4]
3 Write a statement to rename rows and columns together for the dataframe.
df.rename(columns={'City':'Location','Email':'ContactDetails','Fees':'Charges'}, index={0:1,1:2,2:3,3:4})
4 Write statement to display top and botton 3 rows from dataframe.
Sol.:
print(df.head(3)) or print(df.head(n=3))
print(df.head(3)) or df.tail(n=3)
Click here to read topic wise notes, assignments and practical questions for class 12 IP.
Thank you reading the complete article. Share your feedback and share this article in your circle. Leave your reply about this post in comment section.
Plz give answers of this questions.
Solutions already provided