Topics Covered
Short Answer Questions (1 Mark questions)
Take a look on this article before going through questions and answers. Delete rows and columns from DataDrame
MCQs
- Which of the following method is used to delete row/column from a dataframe?
- delete()
- remove()
- discard()
- drop()
- To delete a row from dataframe, which of the following method is correct? A dataframe is indexed with names.
- df.drop(df.row_index=’Jay’)
- df.drop(‘Jay’)
- df.drop(df.row=’Jay’)
- df.drop(df[‘Jay’])
- Which of the following method is correct for following dataframe to delete row by specifying index name? dt= ({‘Year1’:[1200,1220,1500]}, {‘Year2’:1800,1700,1400})
df = pd.DataFrame(dt,index=[‘T1′,’T2’])
Which of the following is correct to delete T2 index record from dataframe?- df.drop(df.index[‘T2’])
- df.drop(df.idx=’T2′)
- df.drop(index=’T2′)
- df.drop(idx=’T2′)
- When you perform delete operation on dataframe, it returns a new dataframe always without changing the old dataframe. To avoid this which parameter you will use to change the original dataframe?
- intact = True
- inplace = True
- update = True
- replace = True
- Ankita is working on dataframe wihch has 4 rows and 3 columns. She wrote df.drop(df.index[[1,3]]). Which rows will be deleted from the dataframe?
- Row 1 and Row 3
- Rows between 1 and 3
- Row 2
- All rows from row no. 1 to index row no. 3
- Consider the datafame given in Que. No. 3. What happens when we add this statement df.Year1!=1200?
- It will delete all rows except 1200 value
- It will delete only row with 1200 value
- It will delete keep data with 1200 only
- None of these
- Which parameter is used to add in drop() method to delete columns?
- column = n (Where n is column number)
- col = n (Where n is column number)
- axis = 0
- axis = 1
Fill in the blanks:
- Mr. Subodh want to keep only three rows from top in his datframe by using a slice with dataframe. So he will write _________ to do the same.
- Ms. Nimisha want to ignore last three rows from dataframe by using a slice with dataframe. So she will wirte _______ to do the same.
- A dataframe has 4 columns named ID, Cname, PhoneNo and City. To delete PhoneNo and city column from the list using columnnames as parameter, ____________ statement will be used.
- To delete rows using list of index as a parameter, _________ statement will be used.
- To delete rows and columns together, __________________ statement will be used.
Answers:
MCQs:
- 4. drop()
- 2. df.drop(‘Jay’)
- 3. df.drop(index=’T2′)
- 2. inplace = True
- 1. Delete row 1 and row 3
- It will delete only row with 1200 value
- axis = 1
Fill in the blanks:
- df[:3]
- df[:-3]
- df.drop(columns=[‘PhonNo’,’City’])
- df.drop(df.index[[0,1]]) –> To delete rows 0 & 1
- df.drop(index=df.index[[1,2]],columns=df.columns[1,2])
Descriptive Questions:
Office | Qtr1 | Qtr2 | Qtr3 | |
North | RO | 800 | 350 | 450 |
West | HO | 500 | 450 | 600 |
South | DO | 450 | 550 | 700 |
East | RO | 300 | 200 | 400 |
North | HO | 450 | 380 | 100 |
East | HO | 500 | 700 | 550 |
- What are the ways to delete rows using drop() method?
- What are the ways to delete rows without using drop() method?
- What are the ways to delete columns using drop() method?
- What are the ways to delete columns without using drop() method?
- Consider above dataframe and write statement to
- Delete rows with label East and West.
- Delete columns Office and Qtr3
- Find errors in following statements:
- df.delete([‘north’,’south’])
- df.drop(index.delete=”North”)
- df.drop([“North”,”South”],axis=0)
- df.drop(Qtr1,Qtr2)
- df.drop(df.iloc[‘North’,’Qrt1′])
- df.drop(df.loc[0][‘Qtr4’])
- df.drop(df.index[[‘North’,’West’]])
- df.drop(df!office[‘HO’])