繁体   English   中英

如何删除数据框中具有较低完整性率的某些功能(Python)

[英]How to remove certain features that have a low completeness rate in a Data frame(Python)

我有一个包含450多个变量和50万行的数据框。 但是,有些变量的空值超过90%。 我想删除空行超过90%的功能。

我对变量进行了描述:

数据框:

df = pd.DataFrame({
    'A':list('abcdefghij'),
     'B':[4,np.nan,np.nan,np.nan,np.nan,np.nan, np.nan, np.nan, np.nan, np.nan],
     'C':[7,8,np.nan,4,2,3,6,5, 4, 6],
     'D':[1,3,5,np.nan,1,0,10,7, np.nan, 5],
     'E':[5,3,6,9,2,4,7,3, 5, 9],
     'F':list('aaabbbckfr'),
     'G':[np.nan,8,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan, np.nan, np.nan]})

print(df)
   A    B  C   D  E  F    G
0  a  4.0  7   1  5  a  NaN
1  b  NaN  8   3  3  a  8.0
2  c  NaN  NaN 5  6  a  NaN
3  d  NaN  4  NaN 9  b  NaN
4  e  NaN  2   1  2  b  NaN
5  f  NaN  3   0  4  b  NaN
6  g  NaN  6  10  7  c  NaN
7  h  NaN  5   7  3  k  NaN
8  i  NaN  4  NaN 5  f  NaN
9  j  NaN  6   5  9  r  NaN

描述:

desc = df.describe(include = 'all')
d1 = desc.loc['varType'] = desc.dtypes
d3 = desc.loc['rowsNull'] = df.isnull().sum()
d4 = desc.loc['%rowsNull'] = round((d3/len(df))*100, 2)

print(desc)
                A        B        C        D        E       F        G
count          10        1       10       10       10      10        1
unique         10      NaN      NaN      NaN      NaN       6      NaN
top             i      NaN      NaN      NaN      NaN       b      NaN
freq            1      NaN      NaN      NaN      NaN       3      NaN
mean          NaN        4      5.4      4.3      5.3     NaN        8
std           NaN      NaN  2.22111  3.16403  2.45176     NaN      NaN
min           NaN        4        2        0        2     NaN        8
25%           NaN        4        4      1.5     3.25     NaN        8
50%           NaN        4      5.5      4.5        5     NaN        8
75%           NaN        4     6.75      6.5     6.75     NaN        8
max           NaN        4        9       10        9     NaN        8
varType    object  float64  float64  float64  float64  object  float64
rowsNull        0        9        1        2        0       0        9
%rowsNull       0       90       10       20        0       0       90

在此示例中,我们仅有2个功能可删除“ B”和“ G”。 但是在我的情况下,我发现40个变量的'%rowsNull'大于> 90%,在建模时应如何不考虑这些变量?

我不知道该怎么做。

请帮我。

谢谢。

首先比较缺失的值,然后得到mean (它起作用,因为True的处理类似于1 s),最后通过使用loc进行boolean indexing进行过滤,因为删除了列:

df = df.loc[:, df.isnull().mean() <.9]
print (df)
   A    C     D  E  F
0  a  7.0   1.0  5  a
1  b  8.0   3.0  3  a
2  c  NaN   5.0  6  a
3  d  4.0   NaN  9  b
4  e  2.0   1.0  2  b
5  f  3.0   0.0  4  b
6  g  6.0  10.0  7  c
7  h  5.0   7.0  3  k
8  i  4.0   NaN  5  f
9  j  6.0   5.0  9  r

详细说明

print (df.isnull().mean())
A    0.0
B    0.9
C    0.1
D    0.2
E    0.0
F    0.0
G    0.9
dtype: float64

您可以找到空值超过90%的列并删除

cols_to_drop = df.columns[df.isnull().sum()/len(df) >= .90]
df.drop(cols_to_drop, axis = 1, inplace = True)


    A   C   D   E   F
0   a   7.0 1.0 5   a
1   b   8.0 3.0 3   a
2   c   NaN 5.0 6   a
3   d   4.0 NaN 9   b
4   e   2.0 1.0 2   b
5   f   3.0 0.0 4   b
6   g   6.0 10.0    7   
7   h   5.0 7.0 3   k
8   i   4.0 NaN 5   f
9   j   6.0 5.0 9   r

根据您的代码,您可以执行以下操作

keepCols = desc.columns[desc.loc['%rowsNull'] < 90]
df = df[keepCols]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM