pandas 使用遮罩就位子集数据帧的最佳方法 [英] Pandas best way to subset a dataframe inplace, using a mask
问题描述
我有一个要缩小尺寸的熊猫数据集(删除x下的所有值).
I have a pandas dataset that I want to downsize (remove all values under x).
掩码为df[my_column] > 50
我通常只使用df = df[mask]
,但要避免每次都复制,特别是因为在函数中使用它时容易出错(因为它仅在函数范围内被更改).
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?
我在想一些类似的事情
df.drop(df.loc[mask].index, inplace = True)
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?
推荐答案
您缺少inplace参数:
You are missing the inplace parameter :
df.drop(df[df.my_column < 50].index, inplace = True)
这篇关于 pandas 使用遮罩就位子集数据帧的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!