0

I have a sample data frame read in using pandas. The data has two columns: 'item','label'. While I shuffle the df rows, I want to make sure the shuffled df does not have items that have the same consecutive labels. ie. this is acceptable, because the labels 'a','b', and 'c' are not in consecutive order:

1: fire, 'a'

2: smoke, 'b'

3: honey bee, 'a'

4: curtain, 'c'

but I want to avoid having such that the labels are in consecutive index, ie:

  1. fire, 'a'

  2. honey bee, 'a'

  3. smoke, 'b'

  4. curtain, 'c'

So far, I can shuffle using:

df = df.sample(frac=1).reset_index(drop=True)

I have a vague idea of looping over until df['label'][i+1] != df['label'][i], but not sure exactly how to. Any pointers or easier suggestion would be appreciated!

Vinci
  • 365
  • 1
  • 6
  • 16
  • This isn't exactly shuffling, but you can reorder the data using `groupby` and `cumcount`. Pandas `sample` does not have any way of doing this for you directly. – cs95 Jan 30 '19 at 22:59
  • pandas doesn't have such capabilities. You may find [this similar question](https://stackoverflow.com/questions/35783551/shuffling-a-list-with-restrictions-in-python) useful. – Qusai Alothman Jan 31 '19 at 16:57

1 Answers1

0

Thanks for the comments/pointers. I got it to work by:

randomized = False
while not randomized:
    xlist = xlistbase.sample(frac=1).reset_index(drop=True) # where xlistbase is the original file read in
    # check for repeats
    for i in range(0, len(xlist)):
        try:
            if i == len(xlist) - 1:
                randomized = True
            elif xlist['label'][i] != xlist['label'][i+1]:
                continue
            elif xlist['label'][i] == xlist['label'][i+1]:
                break
        except IndexError:
            pass
Vinci
  • 365
  • 1
  • 6
  • 16