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']

Python Pandas

4 Likes

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

Answered By

3 Likes


Related Questions