Understanding inplace=True

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

    inplace=True makes the function impure. It changes the original dataframe and returns None. In that case, You breaks the DSL chain. Because most of dataframe functions return a new dataframe, you can use the DSL conveniently. Like

    df.sort_values().rename().to_csv()
    

    Function call with inplace=True returns None and DSL chain is broken. For example

    df.sort_values(inplace=True).rename().to_csv()
    

    will throw NoneType object has no attribute 'rename'

    Something similar with python’s build-in sort and sorted. lst.sort() returns None and sorted(lst) returns a new list.

    Generally, do not use inplace=True unless you have specific reason of doing so. When you have to write reassignment code like df = df.sort_values(), try attaching the function call in the DSL chain, e.g.

    df = pd.read_csv().sort_values()...
    

提交回复
热议问题