Say I have the df:
Name Sequence
Bob IN,IN
Marley OUT,IN
Jack IN,IN,OUT,IN
Harlow
The df has names, and sequences of 'ins/outs'. There can be blank values in the Sequence column. How can I apply these two functions on the Sequence column in an efficient manner? Something like this pseudocode:
df['Sequence'] = converter(sequencer(df['Sequence']))
# takes string of IN/OUT, converts to bits, returns bitstring. 'IN,OUT,IN' -> '010'
def sequencer(seq):
# 'IN,IN' -> ['IN', 'IN']
seq = seq.split(',')
# get sequence up to 3 unique digits. [0,0,1,1,0] = sequence 010
seq = [1 if x == 'IN' else 0 for x in seq]
a = seq[0]
try:
b = seq.index(1-a, 1)
except:
return str(a)
if a not in seq[b+1]:
return str(a) + str(1-a)
return str(a) + str(1-a) + str(a)
# converts bitstring back into in/out format
def converter(seq):
return '-'.join(['IN' if x == '1' else 'OUT' for x in seq])
to result in this dataframe?
Name Sequence
Bob IN
Marley OUT-IN
Jack IN-OUT-IN
Harlow
I glanced at this post here and the comments say to not use apply because it's inefficient and I need efficiency since I'm working on a large dataset.