Python Pandas - Understanding inplace=True

回眸只為那壹抹淺笑 提交于 2019-11-26 13:02:24

When inplace=True is passed, the data is renamed in place (it returns nothing), so you'd use:

df.an_operation(inplace=True)

When inplace=False is passed (this is the default value, so isn't necessary), performs the operation and returns a copy of the object, so you'd use:

df = df.an_operation(inplace=False) 

So:

if inplace == False:
    Assign your result to a new variable
else
    No need to assign

The way I use it is

# Have to assign back to dataframe (because it is a new copy)
df = df.some_operation(inplace=False) 

Or

# No need to assign back to dataframe (because it is on the same copy)
df.some_operation(inplace=True)

CONCLUSION:

 if inplace is False
      Assign to a new variable;
 else
      No need to assign

I usually use with numpy.

you use inplace=True, if you don't want to save the updated data to the same variable

data["column1"].where(data["column1"]< 5, inplace=True)

this is same as...

data["column1"] = data["column1"].where(data["column1"]< 5)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!