同时对行和列应用Pandas函数进行置信区间计算

2024-10-03 02:47:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python编程新手。 我试图确定我的数据集中的异常值。我已经将数据集转换为一个数据帧,然后应用IQR原理。 在那之后,我想用零代替我的离群值,然后计算平均值和标准差作为离群值,作为平均值和标准差的偏差。你知道吗


数据集代码如下:

import pandas as pd
data = [[123,100,1200,800,800,1200,900,1400],[246,15,16,45,15,45,11,55],[234,90,105,180,90,180,100,220],[236,100,90,9000,90,9000,70,140]]
df = pd.DataFrame(data,columns=['ID','Store1','Store2','Store3','Min','Max','Lower_Limit','Upper_limit'])
print (df)

数据集代码段:

    ID  Store1  Store2  Store3  Min   Max  Lower_Limit Upper_limit
  123     100    1200     800  800  1200          900        1400
  246      15      16      45   15    45           11        55
  234      90     105     180   90   180          100          220
  236     100      90    9000   90  9000           70          140

如果Store1、Store2、Store3的值小于Lower\u limit(['Store1']<;['Lower\u limit'])或大于Upper\u limit(['Store1']>;['Upper\u limit']),我想将它们更新为零(0)。你知道吗


以下是我的职责:

def calculate_Outliers(row):
    if row['Store1'] < row['Lower_limit'] or row['Store1'] > row['Upper_limit']:
        return 0
    else:
        return row['Store1']
    if row['Store2'] < row['Lower_limit'] or row['Store2'] > row['Upper_limit']:
        return 0
    else:
        return row['Store2']
    if row['Store3'] < row['Lower_limit'] or row['Store3'] > row['Upper_limit']:
        return 0
    else:
        return row['Store3']

我是这样应用的:

df['Store1','Store3','Store3'] = df.apply(calculate_Outliers, axis=1)

下面是错误的结果。。。你知道吗

    ID  Store1 Store2 Store3(Store1 Store2 Store3)
ID                  
123 NaN NaN NaN NaN 1000
246 NaN NaN NaN NaN 15
234 NaN NaN NaN NaN 0
236 NaN NaN NaN NaN 0

Desired Result:
ID  Store1  Store2  Store3  Min Max Lower_Limit Upper_limit
123 100    1200     800     800 1200    900      1400
246 15     16       45      15  45      11       55
234 0      105      180     90  180    100       220
236 100    90       0       90  9000    70       140

有没有一种方法可以让我修改我的原始代码来实现这一点?你知道吗


Tags: 数据iddfreturnnanminupperlower
3条回答
df.loc[(df['Store1']<df['Lower_Limit']) | (df['Store1']>df['Upper_limit']),['Store1'] ] = 0

对其他商店重复以上步骤。你知道吗

以下功能应完成以下工作:

def calculate_outliers(df):
    df['Store1'][(df['Store1']<df['Lower_Limit']) | (df['Store1'] > df['Upper_limit'])] = 0
    df['Store2'][(df['Store2']<df['Lower_Limit']) | (df['Store2'] > df['Upper_limit'])] = 0
    df['Store3'][(df['Store3']<df['Lower_Limit']) | (df['Store3'] > df['Upper_limit'])] = 0

试试这个:

m=df.filter(like='Store').lt(df.Lower_Limit,axis=0)|df.filter(like='Store').\
                                                     gt(df.Upper_limit,axis=0)

df.update(df.where(~m,0).filter(like='Store'))
print(df)

    ID  Store1  Store2  Store3  Min   Max  Lower_Limit  Upper_limit
0  123       0    1200       0  800  1200          900         1400
1  246      15      16      45   15    45           11           55
2  234       0     105     180   90   180          100          220
3  236     100      90       0   90  9000           70          140

编辑 如果列名没有公共字符串,则可以使用iloc[]

m=df.iloc[:,1:4].lt(df.Lower_Limit,axis=0)|df.iloc[:,1:4].gt(df.Upper_limit,axis=0)
df.update(df.where(~m,0).iloc[:,1:4])
print(df)

    ID  Store1  Store2  Store3  Min   Max  Lower_Limit  Upper_limit
0  123       0    1200       0  800  1200          900         1400
1  246      15      16      45   15    45           11           55
2  234       0     105     180   90   180          100          220
3  236     100      90       0   90  9000           70          140

在函数中换行:

def calculate_Outliers(df):
    m1= df['Store1'].lt(df['Lower_limit'])|df['Store1'].gt(df['Upper_limit'])
    m2 = df['Store2'].lt(df['Lower_limit'])|df['Store2'].gt(df['Upper_limit'])
    m3= df['Store3'].lt(df['Lower_limit'])|df['Store3'].gt(df['Upper_limit'])
    df.loc[m1,'Store1']=0
    df.loc[m1,'Store2']=0
    df.loc[m1,'Store3']=0
    print(df)
calculate_Outliers(df)

    ID  Store1  Store2  Store3  Min   Max  Lower_limit  Upper_limit
0  123       0       0       0  800  1200          900         1400
1  246      15      16      45   15    45           11           55
2  234       0       0       0   90   180          100          220
3  236     100      90    9000   90  9000           70          140

相关问题 更多 >