根据条件熊猫过滤数据帧

时间:2020-01-10 16:23:26

标签: pandas pandas-groupby

我有一个如下所示的数据框


<script>
    $(document).ready(function(){


        var vcc;
        var va;
        var consumo;
        var r1;
        var r2;
        $('#calcular').click(calcularvalores);
        /*Para seleccionar todos los text box*/

        function seleccionar(){
            this.select();
        }


        $('#consumo').click(seleccionar); //Select all text
        $('#vcc').click(seleccionar);
        $('#a').click(seleccionar);
        $('#consumo').keyup(calcularvalores); // WHen user change value
        $('#vcc').keyup(calcularvalores);
        $('#a').keyup(calcularvalores);


        function getvalores(){ //Take what user types
        vcc = document.getElementById("vcc").value;
        va = document.getElementById("a").value;
        consumo = document.getElementById("consumo").value;
        }

        function valores(){ // Parse it to float
        vcc = parseFloat(vcc);
        va = parseFloat(va);
        consumo = parseFloat(consumo);
        }
        function verificar(checkthis){ //Check if stil NaN
            if (isNaN(checkthis)){
                return false;
            } else{ 
                return true;
            }
        }
        function comaAPunto(t1,t2,t3){ // Change , to .
            t1 = t1.replace(",",".");
            t2 = t2.replace(",",".");
            t3 = t3.replace(",",".");


        }

        function calcularvalores(){
            getvalores(); // Get user input

            comaAPunto(va,vcc,consumo); // <-- Error here tryng to change "," to "."

            valores(); // Parsing values to float.
            /* Ecuations implemented */
            consumo = consumo/1000; //Lo convertimos en mA.
            r2 = va/consumo;
            r1 = (vcc-va)/consumo;
            /* FInish ecuations */
            var isok = verificar(va); //Look if user didnt leave it blank or with text inside it.
            if(isok){
            var isreally = verificar(consumo); //Look if fill blank another space.
            if(isreally){
            $('#r11').html("<p>"+r1+"</p>"); //Print values if okay
            $('#r22').html("<p>"+r2+"</p>");
        } else{
            $('#r11').html("<p>Ingresa corriente</p>");
            $('#r22').html("<p>Ingresa corriente</p>");
        }
        } else{
            $('#r11').html("<p>Calculando</p>");
            $('#r22').html("<p>Calculando</p>");
        }
        }
        });
    </script>

从上面可以明显看出,一个属性可能具有多个1个单位。这意味着单位是属性的子类别。

从上述数据中,我想过滤Prop_Usage与Unit_Usage不匹配的行。

预期输出:

Prop_ID    Unit_ID      Prop_Usage      Unit_Usage
1          1            Res             Res
1          2            Res             Com
1          3            Res             Ind
1          4            Res             Res
2          1            Com             Res
2          2            Com             Com
2          3            Com             Com
3          1            Ind             Ind
3          2            Ind             Com

1 个答案:

答案 0 :(得分:0)

您可以结合使用布尔索引和从熊猫中选择.loc来实现。

df = pd.DataFrame([[5,'Res','Res'],[10,'Res','Com']], columns=['var1','Prop_Usage','Unit_Usage'])


df_filtered = df.loc[df.index[df['Prop_Usage'] != df['Unit_Usage']]]

df_filtered包含:

 var1 Prop_Usage Unit_Usage
1    10        Res        Com