Informatics Practices
Find the error in the following code ? Suggest the solution.
>>> topDf
RollNo Name Marks
Sec A 115 Pavni 97.5
Sec B 236 Rishi 98.0
Sec C 307 Preet 98.5
Sec D 422 Paula 98.0
topDf.del['Sec D']
Answer
The error in the code is that topDf.del['Sec D'] is not the correct syntax to delete a row from a DataFrame in pandas. The correct syntax to delete a row in pandas is using the drop() method along with specifying the index label or index position of the row to be deleted.
The corrected code is:
>>> topDf.drop(['Sec D'])
Output
RollNo Name Marks
Sec A 115 Pavni 97.5
Sec B 236 Rishi 98.0
Sec C 307 Preet 98.5
Related Questions
Consider the following dataframe, and answer the questions given below:
import pandas as pd df = pd.DataFrame( { "Quarter1": [2000, 4000, 5000, 4400, 10000], "Quarter2": [5800, 2500, 5400, 3000, 2900], "Quarter3": [20000, 16000, 7000, 3600, 8200], "Quarter4": [1400, 3700, 1700, 2000, 6000]})(i) Write the code to find mean value from above dataframe df over the index and column axis.
(ii) Use sum() function to find the sum of all the values over the index axis.
Write the use of the rename(mapper = <dict-like>, axis = 1) method for a Pandas Dataframe. Can the mapper and columns parameter be used together in a rename() method ?
Find the error in the following code considering the same dataframe topDf given in the previous question.
(i) topDf.rename(index=['a', 'b', 'c', 'd'])
(ii) topDf.rename(columns = {})
Write Python code to create a Series object Temp1 that stores temperatures of seven days in it. Take any random seven temperatures.