Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

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     label

1          1

2         11

3        101

4      10101

5       1001

Here's my expected output

Id     label   result

1          1        0

2         11        0

3        101        1

4      10101        1

5       1001        2

1 Answer

0 votes
by (25.1k points)

You can do the following steps:

  1. Convert the column to string.

  2. Split the resulting strings by ‘1’.

  3. 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

Browse Categories

...