如何将阈值应用于Pandas DataFrame列,并在阈值之外输出一行



我有大量的产品系列数据集。我试图捕获比家族其他成员太高/低的怪异数据条目。例如,我有一个pandas.DataFrame

df =
Prices    Product Family
0    1.99        Yoplait
1    1.89        Yoplait
2    1.59        Yoplait
3    1.99        Yoplait
4    7.99        Yoplait
5    12.99       Hunts 
6    12.99       Hunts 
7    2.99        Hunts 
8    12.49       Hunts

我想编写一个循环,通过每个产品系列进行迭代,设置了某种阈值,这些阈值可以识别哪些产品值得怀疑(第4和第7行),并吐出了该行。我怎样才能做到这一点?

到目前为止,我有这个:

families = df['Product Family'].unique() 
for i in families: 
   if df['Prices] .....(set threshold)
   then.....(spit out that row that is questionable)

,理想情况下,我将为每个产品系列中的for循环中的语句结束。有人对如何设置此阈值并完成代码有一个想法(或更好的想法)吗?

使用pandas时,最好在可能的情况下不使用循环。在您的情况下,我们可以使用groupby()进行类似家庭的操作。这是通过使用与组中位数不同的值来查找离群值的一种方法:

代码:

df['median'] = df.groupby('Product_Family').transform('median')
df['outlier'] = ((df.Prices - df['median']) / df['median']).abs() > 0.5

测试代码:

import pandas as pd
df = pd.read_fwf(StringIO(u"""
    Prices      Product_Family
    1.99        Yoplait
    1.89        Yoplait
    1.59        Yoplait
    1.99        Yoplait
    7.99        Yoplait
    12.99       Hunts 
    12.99       Hunts 
    2.99        Hunts 
    12.49       Hunts"""),
                 skiprows=1)
df['median'] = df.groupby('Product_Family').transform('median')
df['outlier'] = ((df.Prices - df['median']) / df['median']).abs() > 0.5
print(df[df.outlier])    
print(df)

结果:

   Prices Product_Family  median  outlier
4    7.99        Yoplait    1.99     True
7    2.99          Hunts   12.74     True
   Prices Product_Family  median  outlier
0    1.99        Yoplait    1.99    False
1    1.89        Yoplait    1.99    False
2    1.59        Yoplait    1.99    False
3    1.99        Yoplait    1.99    False
4    7.99        Yoplait    1.99     True
5   12.99          Hunts   12.74    False
6   12.99          Hunts   12.74    False
7    2.99          Hunts   12.74     True
8   12.49          Hunts   12.74    False

好吧,我想我的方式与斯蒂芬·劳赫(Stephen Rauch)的方式相似。唯一的区别是我标准化/归一化prices

# Standardize or normalize the `Prices` per `ProductFamily` (absolute value)
df_std = df.groupby('ProductFamily').transform(lambda x: np.abs((x - x.mean()) / x.std()))
# We assume that any Price beyond one standard deviation is an outlier
outlier_mask = df_std['Prices'] > 1.0
# Split clean and outlier dataframes
df_clean = df[~outlier_mask]
df_outlier = df[outlier_mask]

也可以使用分位数与其他答案中的分组和转换一起进行分组和转换。以下使用0.05和0.95分位数作为限制:

# FIND LOWER AND UPPER LIMITS: 
df["lower"] = df.groupby("ProductFamily").transform(lambda x: x.quantile(0.05))
df["upper"] = df.iloc[:,0:2].groupby("ProductFamily").transform(lambda x: x.quantile(0.95))
print(df) 
# SELECT ROWS THAT MEET CRITERIA: 
df = df[(df.Prices > df.lower) & (df.Prices < df.upper)]
print(df)
# TO KEEP ORIGINAL 2 COLUMNS:
df = df.iloc[:,0:2]
print(df)

输出:

   Prices ProductFamily  lower  upper
0    1.99       Yoplait  1.650   6.79
1    1.89       Yoplait  1.650   6.79
2    1.59       Yoplait  1.650   6.79
3    1.99       Yoplait  1.650   6.79
4    7.99       Yoplait  1.650   6.79
5   12.99         Hunts  4.415  12.99
6   12.99         Hunts  4.415  12.99
7    2.99         Hunts  4.415  12.99
8   12.49         Hunts  4.415  12.99
   Prices ProductFamily  lower  upper
0    1.99       Yoplait  1.650   6.79
1    1.89       Yoplait  1.650   6.79
3    1.99       Yoplait  1.650   6.79
8   12.49         Hunts  4.415  12.99
   Prices ProductFamily
0    1.99       Yoplait
1    1.89       Yoplait
3    1.99       Yoplait
8   12.49         Hunts

最新更新