Poka
Poka

Reputation: 387

How to filter data from csv file based on certain conditions using pandas dataframe

I have below data. These are satellite number ,its state and value. Here Value of status 'B' is changing either 0 or some value. Value of status 'A' is always 0. Sat with status A and Val=0 for corresponding status 'B'is not acceptable and not counted.

My aim is to iterate through each row and find correct Satellite present at each row. If any row is all A or B=0 that row is not counted.

So my desire output is : a=3,1,1,1 Count=4 ,Row 4 is not counted

sv-01   sv-02  SV-03  state-01 state-02 state-03    val-01   val-02  val-03
 7        12     8         B          B         B     .23     0.34    1.03
 7        12     8         B          B         A     .35     0       0
 7        12     8         B          A         A     1.45    0       0
 7        12     8         A          A         A      0      0       0
 7        12     8         A          B         B      0      0     3.21

# My python implementation is

  mask = read_data.filter(like='state').eq('A')
  result_count_Nj1 = mask.sum(axis=1).rsub(3)

# I have tried

mask = read_data.filter(like='state').eq('A')  and read_data.filter(like='state').eq('B'!=0)

# But it shows error. Please suggest where I am making mistake

Thanks

Upvotes: 0

Views: 823

Answers (1)

jezrael
jezrael

Reputation: 863116

I believe you need:

#check B values
maskB = read_data.filter(like='state') == 'B'
print (maskB)
   state-01  state-02  state-03
0      True      True      True
1      True      True     False
2      True     False     False
3     False     False     False
4     False      True      True

#check not 0 values for B values only
mask0 = read_data.filter(like='val').where(maskB.values, 0) != 0
print (mask0)
   val-01  val-02  val-03
0    True    True    True
1    True   False   False
2    True   False   False
3   False   False   False
4   False   False    True

a = mask0.sum(1)
print (a)
0    3
1    1
2    1
3    0
4    1
dtype: int64

b = mask0.any(1).sum()
print (b)
4

Upvotes: 1

Related Questions