CBSE Class 12 Informatics Practices
Question 146 of 167
Python Pandas — I — Question 36
Back to all questions(i) The line topDf.rename(index=['a', 'b', 'c', 'd']) attempts to rename the index of the DataFrame topDf, but it doesn't assign the modified DataFrame back to topDf or use the inplace = True parameter to modify topDf directly. Additionally, using a list of new index labels without specifying the current index labels will result in an error.
The corrected code is:
topDf.rename(index={'Sec A': 'a', 'Sec B': 'b', 'Sec C': 'c', 'Sec D': 'd'}, inplace = True)(ii) The line topDf.rename(columns={}) attempts to rename columns in the DataFrame topDf, but it provides an empty dictionary {} for renaming, which will not perform any renaming. We need to provide a mapping dictionary with old column names as keys and new column names as values. To modify topDf directly, it should use the inplace = True parameter.
The corrected code is:
topDf.rename(columns={'RollNo': 'NewRollNo', 'Name': 'NewName', 'Marks': 'NewMarks'}, inplace = True)