Dask Dataframe将列表的列拆分为多列 [英] Dask Dataframe split column of list into multiple columns
本文介绍了Dask Dataframe将列表的列拆分为多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用pb
The same task in Pandas can be easily done with
import pandas as pd
df = pd.DataFrame({"lists":[[i, i+1] for i in range(10)]})
df[['left','right']] = pd.DataFrame([x for x in df.lists])
但是我不知道如何使用 dask.dataframe
But I can't figure out how to do something similar with a dask.dataframe
更新
到目前为止,我已经找到了解决方法
So far I found this workaround
ddf = dd.from_pandas(df, npartitions=2)
ddf["left"] = ddf.apply(lambda x: x["lists"][0], axis=1, meta=pd.Series())
ddf["right"] = ddf.apply(lambda x: x["lists"][1], axis=1, meta=pd.Series())
我想知道是否还有其他方法可以继续。
I'm wondering if there is another way to procede.
推荐答案
您可以使用分配
:
ddf = ddf.assign(left=ddf.lists.map(lambda x: x[0]),
right=ddf.lists.map(lambda x: x[1]))
例如,
ddf.compute()
lists left right
0 [0, 1] 0 1
1 [1, 2] 1 2
2 [2, 3] 2 3
3 [3, 4] 3 4
4 [4, 5] 4 5
5 [5, 6] 5 6
6 [6, 7] 6 7
7 [7, 8] 7 8
8 [8, 9] 8 9
9 [9, 10] 9 10
短语的另一种表达方式(见下面的评论)可能是
An alternative way of phrasing this (see comments, below) might be
ddf = ddf.assign(**{k: ddf.lists.map(lambda x, i=i: x[i])
for i, k in enumerate(['left', 'right'])})
这篇关于Dask Dataframe将列表的列拆分为多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文