简体   繁体   中英

How to apply multiple functions onto a single DataFrame column?

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.

itertools

  • use groupby to get unique (non-repeated) things
  • use islicde to get the first 3.

from itertools import islice, groupby

def f(s):
    return '-'.join([k for k, _ in islice(groupby(s.split(',')), 3)])

df.assign(Sequence=[*map(f, df.Sequence.fillna(''))])

     Name   Sequence
0     Bob         IN
1  Marley     OUT-IN
2    Jack  IN-OUT-IN
3  Harlow           

Variation with a better closure for maximum future flexibility.

from itertools import islice, groupby

def get_f(n, splitter=',', joiner='-'):
    def f(s):
        return joiner.join([k for k, _ in islice(groupby(s.split(splitter)), n)])
    return f

df.assign(Sequence=[*map(get_f(3), df.Sequence.fillna(''))])

Another variation that makes it more obvious what I'm doing (less obnoxious Python bling)

from itertools import islice, groupby

def get_f(n, splitter=',', joiner='-'):
    def f(s):
        return joiner.join([k for k, _ in islice(groupby(s.split(splitter)), n)])
    return f

f = get_f(3)
df['Sequence-InOut'] = [f(s) for s in df.Sequence.fillna('')]
df

     Name      Sequence Sequence-InOut
0     Bob         IN,IN             IN
1  Marley        OUT,IN         OUT-IN
2    Jack  IN,IN,OUT,IN      IN-OUT-IN
3  Harlow          None               

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM