Q. Why is the following code not renaming the index and columns even when code is executing without any error, considering that the sale Df dataframe is as shown on the right.


>>> saleDf

    Target    Sales
zoneA    56000    58000
zoneB    70000    68000
zoneC    75000    78000
zoneD    60000    61000

The code:

saleDf.rename(index = {'zoneC': 'Central', 'zoneD': 'Dakshin'},\
columns = {'Target': 'Targeted', 'Sales': 'Achieved'})
print( saleDf)


What output would be produced by the above code and what is the problem with the code?


Answer :-

The above code will produce the output as :


    Target    Sales
zoneA    56000    58000
zoneB    70000    68000
zoneC    75000    78000
zoneD    60000    61000

which means the given data frame is shown unchanged.

There is no syntax error in the code and internally the line 1 of the code does rename the indexes and columns but since inplace argument is not given, it made the changes in a new dataframe and the original dataframe remained unchanged. So when the line 2 printed the original dataframe saleDf, it showed the unchanged dataframe.

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post