Skip to content
Advertisement

How to find if if a particular column has zero value in a dataframe?

Trust you all are doing well!

I have this dataframe that contains 0 and float numbers in column (‘BP_MOVE’) , there could be two conditions

  1. Zero in any row of column (‘BP_MOVE’)
  2. Zero in each row of column (‘BP_MOVE’)

enter image description here

Below is what i have tried, the first python statement covers the second case very well where each row has 0 value but it fails to cover the first case. similarly, the second python statement gives the following error, is there a way to hit two targets with one arrow ?

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

if (df['bp_move'] == 0).all():
if (df == 0).any(axis=1):

Advertisement

Answer

As the comment fros mosc9576 said, you can apply the same syntax you had int the .all()

if (df['bp_move'] == 0).all():
    print('all')
if (df['bp_move'] == 0).any():
    print('any')
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement