educative.io

Educative

Why after reindexing rows it has to be saved in a new df?

So my question is very straightforward why do we have to save the 'df .reindex() into another dataframe ‘df2’ before reindexing the columns. Does this mean the df.reindex() does not work on the original dataframe?

This is given in the topic ‘Reindexing objects’


Type your question above this line.

Course: https://www.educative.io/collection/10370001/5982561936343040
Lesson: https://www.educative.io/collection/page/10370001/5982561936343040/4778217102639104

Hi @Ambuj_Choudha

The pandas.DataFrame.reindex method takes in a dataframe and returns a new dataframe with the updated index. In the example in the lesson, we’re saving this new dataframe in a new variable df2, but we could have also saved it in the original variable df, both would have been fine.

Hope this helped!