Learner
Learner

Reputation: 691

Get non zero values for each column in pandas

I have pandas dataframe as df:

accel access adviser afpif  afp   publish  afraid verizon
0.00  0.14    0.00   0.00   0.00   0.13    0.00   0.44
0.13  0.00    0.00   0.77   0.00   0.00    0.22   0.00
0.00  0.00    0.87   0.00   0.34   0.00    0.00   0.00
......................................................
.....................................................

I also have a list L which consist columns names as elements

L=['accel','afp','publish']

All I want to extract non zero values of these list elements based on pandas dataframe.

Expected Output:-

dictionary={'accel':0.13,'afp':0.34,'publish':0.13}

Upvotes: 4

Views: 7610

Answers (3)

Mysterious
Mysterious

Reputation: 881

One line answer would be:

df.sum().to_dict()

Upvotes: -3

Vivek Kalyanarangan
Vivek Kalyanarangan

Reputation: 9081

Thanks to @jpp, not using chained indexing and using .loc instead -

op = { col: df.loc[df[col].ne(0), col].tolist() for col in L }

Output -

{'accel': [0.13], 'afp': [0.34], 'publish': [0.13]}

This implementation will be more robust in the sense it can retrieve multiple non-zero values. However, if you are sure you just want the one value, you can filter it out inside the dict comprehension itself -

op = { col: df.loc[df[col].ne(0), col].iat[0] for col in L }

OR

op = { col: df.loc[df[col].ne(0), col].values[0] for col in L }

Output -

{'accel': 0.13, 'afp': 0.34, 'publish': 0.13}

Note: If you are sure your non-zero values are positive, you can use >0 or the Series.gt() API

Upvotes: 1

jezrael
jezrael

Reputation: 862611

Use DataFrame.loc with dict comprehension and iat if always exist at least one non 0 value:

d = {c: df.loc[df[c] ! =0, c].iat[0] for c in L }
print (d)
{'accel': 0.13, 'afp': 0.34, 'publish': 0.13}

More general working with only 0 columns too:

d = {c: next(iter(df.loc[df[c] != 0, c]), 'no value') for c in L }
print (d)
{'accel': 0.13, 'afp': 0.34, 'publish': 0.13}

Upvotes: 4

Related Questions