【问题标题】:compare two data frames and delete columns based on lookup table比较两个数据框并根据查找表删除列
【发布时间】:2018-08-05 10:56:11
【问题描述】:

我有两个数据框:

df1:

    A   B   C   D   E   F
0   63  9   56  23  41  0
1   40  35  69  98  47  45
2   51  95  55  36  10  34
3   25  11  67  83  49  89
4   91  10  43  73  96  95
5   2   47  8   30  46  9
6   37  10  33  8   45  20
7   40  88  6   29  46  79
8   75  87  49  76  0   69
9   92  21  86  91  46  41

df2:

    A   B   C   D   E   F
0   0   0   0   1   1   0

我想根据 df2(查找表)中的值删除 df1 中的列。只要 df2 有 1,我就必须删除 df1 中的那一列。

所以我的最终输出应该是这样的。

    A   B   C   F
0   63  9   56  0
1   40  35  69  45
2   51  95  55  34
3   25  11  67  89
4   91  10  43  95
5   2   47  8   9
6   37  10  33  20
7   40  88  6   79
8   75  87  49  69
9   92  21  86  41

【问题讨论】:

    标签: python python-3.x pandas dataframe


    【解决方案1】:

    假设len(df1.columns) == len(df2.columns)

    df1.loc[:, ~df2.loc[0].astype(bool).values]
    
        A   B   C   F
    0  63   9  56   0
    1  40  35  69  45
    2  51  95  55  34
    3  25  11  67  89
    4  91  10  43  95
    5   2  47   8   9
    6  37  10  33  20
    7  40  88   6  79
    8  75  87  49  69
    9  92  21  86  41
    

    如果列不相同,但df2df1 中有列的子集,则

    df1.reindex(df2.columns[~df2.loc[0].astype(bool)], axis=1)
    

    或者用drop,类似@student的方法:

    df1.drop(df2.columns[df2.loc[0].astype(bool)], axis=1)
    

        A   B   C   F
    0  63   9  56   0
    1  40  35  69  45
    2  51  95  55  34
    3  25  11  67  89
    4  91  10  43  95
    5   2  47   8   9
    6  37  10  33  20
    7  40  88   6  79
    8  75  87  49  69
    9  92  21  86  41
    

    【讨论】:

    • 您好,感谢您的帮助,如果列不同怎么办。
    • @BhanuTez 查看编辑。只要列数相同,就可以了。
    【解决方案2】:

    列可以做intersection

    df1[df1.columns.intersection(df2.columns[~df2.iloc[0].astype(bool)])]
    Out[354]: 
        A   B   C   F
    0  63   9  56   0
    1  40  35  69  45
    2  51  95  55  34
    3  25  11  67  89
    4  91  10  43  95
    5   2  47   8   9
    6  37  10  33  20
    7  40  88   6  79
    8  75  87  49  69
    9  92  21  86  41
    

    【讨论】:

      【解决方案3】:

      您可以尝试使用drop 删除列:

      remove_col = df2.columns[(df2 == 1).any()] # get columns with any value 1
      df1.drop(remove_col, axis=1, inplace=True) # drop the columns in original dataframe
      

      或者,在一行中:

      df1.drop(df2.columns[(df2 == 1).any()], axis=1, inplace=True)
      

      【讨论】:

        【解决方案4】:

        以下是一个易于理解的解决方案:

        df1.loc[:,df2.loc[0]!=1]
        

        输出:

            A   B   C   F
        0  63   9  56   0
        1  40  35  69  45
        2  51  95  55  34
        3  25  11  67  89
        4  91  10  43  95
        5   2  47   8   9
        6  37  10  33  20
        7  40  88   6  79
        8  75  87  49  69
        9  92  21  86  41
        

        loc 可用于选择具有布尔或条件查找的行或列:https://www.shanelynn.ie/select-pandas-dataframe-rows-and-columns-using-iloc-loc-and-ix/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-10-19
          • 2012-03-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-07
          • 1970-01-01
          相关资源
          最近更新 更多