【问题标题】:How to delete rows from column which have matching values in the list Pandas如何从 Pandas 列表中具有匹配值的列中删除行
【发布时间】:2020-07-06 03:22:53
【问题描述】:

我正在从列中查找异常值并将它们存储在列表中。现在我想删除所有的值 列在我的列表中。 怎样才能做到这一点?

这是我查找异常值的函数

outlier=[]

def detect_outliers(data):

    threshold=3
    m = np.mean(data)
    st = np.std(data)

    for i in data:
        #calculating z-score value
        z_score=(i-m)/st
        #if the z_score value is greater than threshold value than its a outlier
        if np.abs(z_score)>threshold:
            outlier.append(i)
    return outlier

This is my column in data frame

df_train_11.AMT_INCOME_TOTAL

【问题讨论】:

    标签: pandas numpy machine-learning scikit-learn


    【解决方案1】:
    import numpy as np, pandas as pd
    
    df = pd.DataFrame(np.random.rand(10,5))
    
    outlier_list=[]
    def detect_outliers(data):
        threshold=0.5
        for i in data:
        #calculating z-score value
            z_score=(df.loc[:,i]- np.mean(df.loc[:,i])) /np.std(df.loc[:,i])
            outliers = np.abs(z_score)>threshold
    
            outlier_list.append(df.index[outliers].tolist())
        return outlier_list
    
    outlier_list = detect_outliers(df)
    
    [[1, 2, 4, 5, 6, 7, 9],
     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
     [0, 1, 2, 4, 8],
     [0, 1, 3, 4, 6, 8],
     [0, 1, 3, 5, 6, 8, 9]]
    

    这样,您可以获得每列的异常值。 outlier_list[0] 给你[1, 2, 4, 5, 6, 7, 9] 这意味着第 1,2 行等是第 0 列的异常值。

    编辑

    简短的回答:

    
    df = pd.DataFrame(np.random.randn(10, 3), columns=list('ABC'))
    df[((df.B - df.B.mean()) / df.B.std()).abs() < 3]
    

    这将过滤只有一列(例如“B”)在三个标准偏差内的 DataFrame。

    【讨论】:

    • 如何去除异常值?
    • 查看我编辑的答案。您还需要选择一列,据此检测异常值
    猜你喜欢
    • 2021-12-11
    • 2016-05-06
    • 2022-08-13
    • 2016-03-13
    • 1970-01-01
    • 2019-12-22
    • 2022-01-26
    • 2019-02-14
    • 1970-01-01
    相关资源
    最近更新 更多