代码之家  ›  专栏  ›  技术社区  ›  Shuvayan Das

如何删除使用后生成的重复列警察局的傻瓜用它们的方差作为分界点

  •  1
  • Shuvayan Das  · 技术社区  · 6 年前

    df_target = pd.get_dummies(df_column[column], dummy_na=True,prefix=column)
    

    其中column是一个列名,df\u column是一个数据帧,从中提取每一列来执行一些操作。

    rev_grp_m2_> 225    rev_grp_m2_nan  rev_grp_m2_nan
    0                       0                   0
    0                       0                   0
    0                       0                   0
    0                       0                   0
    0                       0                   0
    0                       0                   0
    0                       0                   0
    1                       0                   0
    0                       0                   0
    0                       0                   0
    0                       0                   0
    0                       0                   0
    

    for target_column in list(df_target.columns):
        # If variance of the dummy created is zero : append it to a list and print to log file.
        if ((np.var(df_target_attribute[[target_column]])[0] != 0)==True):
            df_final[target_column] = df_target[target_column]
    

    这里由于两列是相同的,我得到了净现值线路。

    erev_grp_m2_nan    0.000819
    rev_grp_m2_nan    0.000000
    

    理想情况下,我会选择一个非零方差和删除/跳过一个0变量。

    1 回复  |  直到 6 年前
        1
  •  1
  •   jezrael    6 年前

    为了 DataFrame.var 用途:

    print (df.var())
    rev_grp_m2_> 225    0.083333
    rev_grp_m2_nan      0.000000
    rev_grp_m2_nan      0.000000
    

    最后一个用于过滤 boolean indexing :

    out = df.loc[:, df.var()!= 0]
    print (out)
        rev_grp_m2_> 225
    0                  0
    1                  0
    2                  0
    3                  0
    4                  0
    5                  0
    6                  0
    7                  1
    8                  0
    9                  0
    10                 0
    11                 0
    

    iloc :

    cols = [i for i in np.arange(len(df.columns)) if np.var(df.iloc[:, i]) != 0]
    print (cols)
    [0]
    
    df = df.iloc[:, cols]
    print (df)
        rev_grp_m2_> 225
    0                  0
    1                  0
    2                  0
    3                  0
    4                  0
    5                  0
    6                  0
    7                  1
    8                  0
    9                  0
    10                 0
    11                 0
    

    另一个想法是过滤掉所有的值 0

    cols = [i for i in np.arange(len(df.columns)) if (df.iloc[:, i] != 0).any()]
    out = df.iloc[:, cols]
    

    或:

    out = df.loc[:, (df != 0).any()]
    print (out)
        rev_grp_m2_> 225
    0                  0
    1                  0
    2                  0
    3                  0
    4                  0
    5                  0
    6                  0
    7                  1
    8                  0
    9                  0
    10                 0
    11                 0