0

I have a problem with segmenting a pd.DataFrame (or pd.Series) at a point the value switches.

For example, I have a data like

list('AAAAABBBBBAAABBBCCCCBBBBAAAAA')

as you can see, As and Bs and Cs are arranged in chunks.

What is the most pythonic way to manage them in to segments?

I believe the code will be produce a array-like data that is like:

list('00000111112223334445555666677777')

which is a list of the names for the segments.

1 Answer 1

1

Most pythonic way would be using itertools.groupby()

But your output format needs some work as it only holds 10 numbers (0-9) what's the difference between character '11' and '1' when it's repeated several times like that.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot! my output format was just to avoid the lousy [0,0,0,0,1, ... ] which could have been veeery long.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.