Pandas best way to subset a dataframe inplace, using a mask

青春壹個敷衍的年華 提交于 2019-12-06 02:02:07

问题


I have a pandas dataset that I want to downsize (remove all values under x).

The mask is df[my_column] > 50

I would typically just use df = df[mask], but want to avoid making a copy every time, particularly because it gets error prone when used in functions (as it only gets altered in the function scope).

What is the best way to subset a dataset inplace?

I was thinking of something along the lines of
df.drop(df.loc[mask].index, inplace = True)

Is there a better way to do this, or any situation where this won't work at all?


回答1:


You are missing the inplace parameter :

df.drop(df[df.my_column < 50].index, inplace = True)




回答2:


I think this works. Maybe there are better ways?

df = df.drop(df[df.my_column < 50].index)



来源:https://stackoverflow.com/questions/33103988/pandas-best-way-to-subset-a-dataframe-inplace-using-a-mask

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