What is the most efficient method to apply a function to a column in a dask dataframe?

I have a function that tokenises words from a tuple:

def get_word_tokens(tokens):
    words = [token[0] for token in tokens]
    return words

I want to apply this to column in a dask dataframe and create a new column e.g.

df1

    #phrase               tokens  
0   call CHRIS MOBILE.    [(call, 0, 4), 
                          (CHRIS, 5, 10), 
                          (MOBILE, 11, 17)]
1   call Tod Sarks        [(call, 0, 4), 
                          (Tod, 5, 8), 
                          (arks, 9, 14)]       

Create column words

df1

    #phrase               tokens               words
0   call CHRIS MOBILE.    [(call, 0, 4),       call, CHRIS, MOBILE
                          (CHRIS, 5, 10), 
                          (MOBILE, 11, 17)]
1   call Tod Sarks        [(call, 0, 4),       call, Tod, Sarks
                          (Tod, 5, 8), 
                          (Sarks, 9, 14)]   

I have tried:

df['words'] = df.apply(lambda row: get_word_tokens(df['tokens']), axis = 1)

This appears to be working but is taking a very long time to run? Is there a faster method?

Answer

You are passing df['tokens'] to the function, which is the full column. This should work:

def get_word_tokens(tokens):
    words = [token[0] for token in tokens]
    return words

data = [
    ['call CHRIS MOBILE.', [('call', 0, 4), 
                          ('CHRIS', 5, 10), 
                          ('MOBILE', 11, 17)]],
    ['call Tod Sarks', [('call', 0, 4), 
                          ('Tod', 5, 8), 
                          ('arks', 9, 14)]],
]

import pandas as pd
df = pd.DataFrame(data, columns=['phrase', 'tokens'])
df = pd.concat([df,df,df,df, df, df])

import dask.dataframe as dd
ddf = dd.from_pandas(df, npartitions=2)

def get_word_tokens_df(df):
    df['words'] = df['tokens'].apply(get_word_tokens)
    return df


ddf = ddf.map_partitions(get_word_tokens_df)
ddf.compute()

Leave a Reply

Your email address will not be published. Required fields are marked *