English

What if in the rename function we pass a value for a row label that does not exist?

Advertisements
Advertisements

Question

What if in the rename function we pass a value for a row label that does not exist?

Code Writing
Advertisements

Solution

If we pass a row label that does not exist in the rename() function, then no change will occur and the DataFrame will remain the same. he rename() function only changes the labels that are already present in the DataFrame. It ignores the labels that do not exist.

import pandas as pd
df = pd.DataFrame({'Marks': [85, 90, 78]}, index=['a', 'b', 'c'])
df_new = df.rename(index={'b': 'Beta', 'z': 'Omega'})
print(df_new)

Output:

  Marks
a 85
Beta 90
c 78
shaalaa.com
  Is there an error in this question or solution?
Chapter 2: Data Handling using Pandas - I - Intext Questions [Page 48]

APPEARS IN

NCERT Informatics Practices [English] Class 12
Chapter 2 Data Handling using Pandas - I
Intext Questions | Q 11. | Page 48
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×