Amrith Krishna Amrith Krishna - 1 year ago 111
Python Question

Find rows with non zero values in a subset of columns in pandas dataframe

I have a datframe with 4 columns of strings and others as integers. Now I need to find out those rows of data where at least one of the column is a non-zero value (or > 0).

manwra, akriti,T5,0,0,1,0,K
awma, prabrtih,B6, 0,1,1,0,S

My output should be

manwra, akriti,T5,0,0,1,0,K
awma, prabrtih,B6, 0,1,1,0,S

I have tried the following to obtain the answer. The string values are in colums 0,1,2 and -1 (last column).

KT[KT.ix[:,3:-2] != 0]

What I am receiving as output is


How to obtain the desired output

Answer Source

assume your dataframe is df

df.loc[(df.loc[:, df.dtypes != object] != 0).any(1)]

enter image description here