pythonpandasdata-preprocessing

What is the Pandas equivalent for matlab code A(A(:,4)==0,:)=[];?


The code A(A(:,4)==0,:)=[]; in MATLAB drops rows in matrix A if values in the fourth column are zero.

I tried to write a code of A=A[A[:,4] !==0], given that pandas is called but it was rejected in python.

If the code runs well in python, I expect that it will be ignoring rows where the values in the fourth column are zeros. The original data will not be affected in excel.


Solution

  • Use iloc to select the column and boolean indexing:

    A = A[A.iloc[:, 4] != 0]
    

    Variants:

    A = A[A.iloc[:, 4].ne(0)]
    
    A = A.loc[A.iloc[:, 4] != 0]
    
    # only if the column name is unique
    A = A[A[A.columns[4]] != 0]