按国家/地区在所有属性中应用z分数

问题描述 投票:-1回答:1

我正在尝试清理一个数据集,其中包含2000年至2015年世界上每个国家的数据。按年计算的人口数据非常糟糕-我想按年为每个国家的人口数据分配z分数,以便我可以看到哪些数据点作为异常值而下降。我该怎么做?我以为我需要使用groupby(),但不确定如何部署它。

我正在使用世卫组织Kaggle数据集:https://www.kaggle.com/kumarajarshi/life-expectancy-who/data#

数据通常看起来像这样:

Example

pandas-groupby outliers
1个回答
0
投票

也许,像这样的事情可能有用-

import numpy as np, pandas as pd
l1 = ['a'] * 5 + ['b'] * 10 + ['c'] * 8
l2 = list(np.random.randint(10,20,size=5)) + list(np.random.randint(100,150, size=10)) + list(np.random.randint(75,100, size=8))
df = pd.DataFrame({'cat':l1, 'values':l2}) #creating a dummy dataframe
df
    cat  values
0    a      18
1    a      17
2    a      11
3    a      13
4    a      11
5    b     102
6    b     103
7    b     119
8    b     113
9    b     100
10   b     113
11   b     102
12   b     108
13   b     128
14   b     126
15   c      75
16   c      96
17   c      81
18   c      90
19   c      80
20   c      95
21   c      96
22   c      86

df['z-score'] = df.groupby(['cat'])['values'].apply(lambda x: (x - x.mean())/x.std())
df

     cat  values   z-score
0    a      18     1.206045
1    a      17     0.904534
2    a      11    -0.904534
3    a      13    -0.301511
4    a      11    -0.904534
5    b     102    -0.919587
6    b     103    -0.821759
7    b     119     0.743496
8    b     113     0.156525
9    b     100    -1.115244
10   b     113     0.156525
11   b     102    -0.919587
12   b     108    -0.332617
13   b     128     1.623951
14   b     126     1.428295
15   c      75    -1.520176
16   c      96     1.059516
17   c      81    -0.783121
18   c      90     0.322461
19   c      80    -0.905963
20   c      95     0.936674
21   c      96     1.059516
22   c      86    -0.168908
© www.soinside.com 2019 - 2024. All rights reserved.