Akshay Sachdeva
Akshay Sachdeva

Reputation: 53

Condition in dataframe

I have a data frame which contains values in the following format:

S1  S2  S3  S4  S5  
A   0   0   d   e   
A   b   0   0   0   
A   b   c   0   e   
A   b   c   d   0   
A   b   c   d   e   

I just want the count of the values in a row only if they are continuously present. If only one value is present initially then the count is zero, but if two continuous values are present in a row then it's two:

S1  S2  S3  S4  S5  Count
A   0   0   d   e   zero
A   b   0   0   0   two
A   b   c   0   e   three
A   b   c   d   0   four
A   b   c   d   e   five

Upvotes: 1

Views: 66

Answers (1)

jezrael
jezrael

Reputation: 862521

First compare 0 values by DataFrame.eq for equal (0 means values are numbers, if strings use '0'), get cumulative sum by DataFrame.cumsum per rows and count 0 values by sum, last use Series.map:

d = {0:'no value', 1:'zero', 2:'two', 3:'three', 4:'four', 5:'five'}

df['Count'] = df.eq(0).cumsum(axis=1).eq(0).sum(axis=1).map(d)
print (df)
  S1 S2 S3 S4 S5  Count
0  A  0  0  d  e   zero
1  A  b  0  0  0    two
2  A  b  c  0  e  three
3  A  b  c  d  0   four
4  A  b  c  d  e   five

Upvotes: 1

Related Questions