Understanding inplace=True

后端 未结 11 1090
遥遥无期
遥遥无期 2020-11-22 03:25

In the pandas library many times there is an option to change the object inplace such as with the following statement...

df.dropna(axis=\'index\         


        
11条回答
  •  孤独总比滥情好
    2020-11-22 03:41

    When trying to make changes to a Pandas dataframe using a function, we use 'inplace=True' if we want to commit the changes to the dataframe. Therefore, the first line in the following code changes the name of the first column in 'df' to 'Grades'. We need to call the database if we want to see the resulting database.

    df.rename(columns={0: 'Grades'}, inplace=True)
    df
    

    We use 'inplace=False' (this is also the default value) when we don't want to commit the changes but just print the resulting database. So, in effect a copy of the original database with the committed changes is printed without altering the original database.

    Just to be more clear, the following codes do the same thing:

    #Code 1
    df.rename(columns={0: 'Grades'}, inplace=True)
    #Code 2
    df=df.rename(columns={0: 'Grades'}, inplace=False}
    

提交回复
热议问题