Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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}

Solution 2:

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

Solution 3:

One line answer would be:

df.sum().to_dict()

Post a Comment for "Get Non Zero Values For Each Column In Pandas"