达斯达克(Dask)-如何使用apply将Series串联到DataFrame中?

时间:2019-03-05 01:42:43

标签: python pandas dataframe dask dask-distributed

如何从Dask系列上应用的函数返回多个值? 我试图从dask.Series.apply的每次迭代中返回一个序列,并将最终结果设为dask.DataFrame

以下代码告诉我该元错误。但是,所有熊猫版本都可以使用。怎么了?

更新:我认为我没有正确指定meta / schema。如何正确执行? 现在,当我删除meta参数时,它可以工作。但是,它会发出警告。我想“正确”使用dask。

import dask.dataframe as dd
import pandas as pd
import numpy as np
from sklearn import datasets

iris = datasets.load_iris()

def transformMyCol(x):
    #Minimal Example Function
    return(pd.Series(['Tom - ' + str(x),'Deskflip - ' + str(x / 8),'']))

#
## Pandas Version - Works as expected.
#
pandas_df = pd.DataFrame(data= np.c_[iris['data'], iris['target']], columns= iris['feature_names'] + ['target'])
pandas_df.target.apply(transformMyCol,1)

#
## Dask Version (second attempt) - Raises a warning
#
df = dd.from_pandas(pandas_df, npartitions=10)

unpacked = df.target.apply(transformMyCol)
unpacked.head()

#
## Dask Version (first attempt) - Raises an exception 
#
df = dd.from_pandas(pandas_df, npartitions=10)

unpacked_dask_schema = {"name" : str, "action" : str, "comments" : str}

unpacked = df.target.apply(transformMyCol, meta=unpacked_dask_schema)
unpacked.head()

这是我得到的错误:

  File "/anaconda3/lib/python3.7/site-packages/dask/dataframe/core.py", line 3693, in apply_and_enforce
    raise ValueError("The columns in the computed data do not match"
ValueError: The columns in the computed data do not match the columns in the provided metadata

我也了解了以下内容,但它也不起作用。

meta_df = pd.DataFrame(dtype='str',columns=list(unpacked_dask_schema.keys()))


unpacked = df.FILEDATA.apply(transformMyCol, meta=meta_df)
unpacked.head()

相同错误:

  File "/anaconda3/lib/python3.7/site-packages/dask/dataframe/core.py", line 3693, in apply_and_enforce
    raise ValueError("The columns in the computed data do not match"
ValueError: The columns in the computed data do not match the columns in the provided metadata

1 个答案:

答案 0 :(得分:3)

是的,问题是您没有正确指定元数据;更具体地说,正如错误消息所述,元数据列("name", "action", "comments")与计算数据(0, 1, 2)中的列不匹配。您应该:

  1. 将元数据列更改为0、1、2:
   unpacked_dask_schema = dict.fromkeys(range(3), str)
   df.target.apply(transformMyCol, meta=unpacked_dask_schema)

  1. 更改transformMyCol以使用命名列:

    def transformMyCol(x):
        return pd.Series({
            'name': 'Tom - ' + str(x), 
            'action': 'Deskflip - ' + str(x / 8), 
            'comments': '',
        }))