从pandas.dataframe删除低频值
如何从pandas.DataFrame
很少发生(即频率较低)的列中删除值?例:
In [4]: df[col_1].value_counts()
Out[4]: 0 189096
1 110500
2 77218
3 61372
...
2065 1
2067 1
1569 1
dtype: int64
因此,我的问题是:如何删除like2065, 2067,
1569
和others的值?以及如何对包含.value_counts()
这样的所有列执行此操作?
更新: 关于“低”,我的意思是像2065
。该值出现col_1
1(一)次,我想删除这样的值。
-
我看到您可能有两种方法可以执行此操作。
对于整个DataFrame
此方法删除整个DataFrame中很少出现的值。我们可以使用内置函数来加快处理速度,而无需循环。
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randint(0, high=9, size=(100,2)), columns = ['A', 'B']) threshold = 10 # Anything that occurs less than this will be removed. value_counts = df.stack().value_counts() # Entire DataFrame to_remove = value_counts[value_counts <= threshold].index df.replace(to_remove, np.nan, inplace=True)
逐列
此方法删除每个列中不经常出现的条目。
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randint(0, high=9, size=(100,2)), columns = ['A', 'B']) threshold = 10 # Anything that occurs less than this will be removed. for col in df.columns: value_counts = df[col].value_counts() # Specific column to_remove = value_counts[value_counts <= threshold].index df[col].replace(to_remove, np.nan, inplace=True)