This post and this post have gotten me close, but I haven't been able to solve my problem.
I have a df that looks like:
2017-04-03 2017-04-04 2017-04-05 2017-04-06
id
0 0.0 active 0.0 0.0
1 0.0 active 0.0 active
2 0.0 0.0 0.0 0.0
I want to count the zeros across each row and put them into a string to code the data, but the count needs to reset whenever there aren't consecutive zeros.
For the above df, the output df would look like:
2017-04-03 2017-04-04 2017-04-05 2017-04-06
id
0 inactive_1 active inactive_1 inactive_2
1 inactive_1 active inactive_1 active
2 inactive_1 inactive_2 inactive_3 inactive_4
this function gets me very close, but doesn't account for reseting the cumsum, it just sums for all instances of zero in the row.
def inactive(s):
np.where(s == 0, 'inactive_' + (s.eq(0).cumsum()).astype(str), s)
df.apply(inactive, 1)