栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用dask映射列

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用dask映射列

您可以使用.map方法,就像在Pandas中一样

In [1]: import dask.dataframe as ddIn [2]: import pandas as pdIn [3]: df = pd.Dataframe({'x': [1, 2, 3]})In [4]: ddf = dd.from_pandas(df, npartitions=2)In [5]: df.x.map(lambda x: x + 1)Out[5]: 0    21    32    4Name: x, dtype: int64In [6]: ddf.x.map(lambda x: x + 1).compute()Out[6]: 0    21    32    4Name: x, dtype: int64

元数据

可能会要求您提供一个

meta=
关键字。这使dask.dataframe知道函数的输出名称和类型。从
map_partitions
此处复制文档字符串:

meta : pd.Dataframe, pd.Series, dict, iterable, tuple, optionalAn empty pd.Dataframe or pd.Series that matches the dtypes and column names of the output. This metadata is necessary for many algorithms in dask dataframe to work. For ease of use, some alternative inputs are also available. Instead of a Dataframe, a dict of {name: dtype} or iterable of (name, dtype) can be provided. Instead of a series, a tuple of (name, dtype) can be used. If not provided, dask will try to infer the metadata. This may lead to unexpected results, so providing meta is  recommended.For more information, see dask.dataframe.utils.make_meta.

因此,在上面的示例中,我的输出将是具有name

'x'
和dtype的系列,
int
我可以执行以下任一操作来更明确

>>> ddf.x.map(lambda x: x + 1, meta=('x', int))

要么

>>> ddf.x.map(lambda x: x + 1, meta=pd.Series([], dtype=int, name='x'))

这告诉dask.dataframe对我们的函数有什么期望。如果未提供任何元数据,则dask.dataframe将尝试在少量数据上运行函数。如果失败,将引发错误以寻求帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/647899.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号