English

What would happen if the label or row index passed is not present in the DataFrame?

Advertisements
Advertisements

Question

What would happen if the label or row index passed is not present in the DataFrame?

Code Writing
Advertisements

Solution

If the label or row index passed is not present in the DataFrame, then an error will occur if we try to access or modify it. The DataFrame can only perform operations on existing labels or indexes. If the label does not exist, Pandas cannot find it.

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

APPEARS IN

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

Englishहिंदीमराठी


      Forgot password?
Use app×