user3476463
user3476463

Reputation: 4575

select column with non-zero values from dataframe

I have data like the data below. I would like to only return the columns from the dataframe that contain at least one non-zero value. So in the example below it would be column ALF. Returning non-zero rows doesn’t seem that tricky but selecting the column and records is giving me a little trouble.

print df

Data:

Type             ADR             ALE     ALF               AME  
Seg0              0.0            0.0     0.0              0.0   
Seg1              0.0            0.0     0.5              0.0 

When I try something like the link below:

Pandas: How to select columns with non-zero value in a sparse table

m1 = (df['Type'] == 'Seg0')
m2 = (df[m1] != 0).all()

print (df.loc[m1,m2])

I get a key error for 'Type'

Upvotes: 8

Views: 22451

Answers (1)

jezrael
jezrael

Reputation: 862611

In my opinion you get key error because first column is index:

Solution use DataFrame.any for check at least one non zero value to mask and then filter index of Trues:

m2 = (df != 0).any()
a = m2.index[m2]
print (a)
Index(['ALF'], dtype='object')

Or if need list:

a = m2.index[m2].tolist()
print (a)
['ALF']

Similar solution is filter columns names:

a = df.columns[m2]

Detail:

print (m2)
ADR    False
ALE    False
ALF     True
AME    False
dtype: bool

Upvotes: 8

Related Questions