Back
I want to do I count the longest consecutive '0' flanked by number '1' in is string using pandas dataframe
Here's my dataset
Id label1 12 113 1014 101015 1001
Id label
1 1
2 11
3 101
4 10101
5 1001
Here's my expected output
Id label result1 1 02 11 03 101 14 10101 15 1001 2
Id label result
1 1 0
2 11 0
3 101 1
4 10101 1
5 1001 2
You can do the following steps:
Convert the column to string.
Split the resulting strings by ‘1’.
Count the max.
df['result'] = df.label.astype(str).str.split('1').apply(lambda x: len(max(x)))
31k questions
32.8k answers
501 comments
693 users