Understanding inplace=True

后端 未结 11 1342
遥遥无期
遥遥无期 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:26

    If you don't use inplace=True or you use inplace=False you basically get back a copy.

    So for instance:

    testdf.sort_values(inplace=True, by='volume', ascending=False)
    

    will alter the structure with the data sorted in descending order.

    then:

    testdf2 = testdf.sort_values( by='volume', ascending=True)
    

    will make testdf2 a copy. the values will all be the same but the sort will be reversed and you will have an independent object.

    then given another column, say LongMA and you do:

    testdf2.LongMA = testdf2.LongMA -1
    

    the LongMA column in testdf will have the original values and testdf2 will have the decrimented values.

    It is important to keep track of the difference as the chain of calculations grows and the copies of dataframes have their own lifecycle.

提交回复
热议问题