Tuesday 26 October 2021

Adding two columns in Dask with apply function

I have a Dask function that adds a column to an existing Dask dataframe, this works fine:

df = pd.DataFrame({
    'height':  [6.21, 5.12, 5.85, 5.78, 5.98],
    'weight': [150, 126, 133, 164, 203]
})

df_dask = dd.from_pandas(df, npartitions=2) 


s = """
obj.weight + 100
"""

df_dask['new_weight'] = df_dask.apply(lambda obj: eval(s), meta=dict, axis=1)

Now, I want to add two columns instead of one:

s = """
obj.weight + 100, obj.weight + 200
"""

df_dask['new_weight','new_weight2'] = df_dask.apply(lambda obj: eval(s), meta=dict, axis=1)

But I get

NotImplementedError: Item assignment with <class 'tuple'> not supported

Does this mean that this is not supported or I'm doing something wrong? If not supported, is there a workaround? What I need is to return a list of floats.



from Adding two columns in Dask with apply function

No comments:

Post a Comment