2

Dask 有点新,但由于大多数操作都是懒惰的,我怎样才能使这样的基本案例大规模工作?

import dask.dataframe as dd
import dask.bag as db

dataset = [
    dict(a = 1, b = 2, c = 3),
    dict(a = 3, b = 4, d = 5, e = 5),
    dict(a = 2, x = 1, y = 2, z = 3, q = 5)
    # etc...
]

dag_data = db.from_sequence(dataset)
dag_data.to_dataframe() 

在仅限 Pandas 的世界中,我可以映射pd.Series,但问题是这些操作在计算完之后才会计算。上面的代码生成了一个只有第一条记录(“a”、“b”、“c”)的特征的 DataFrame。

预期结果:

| a | b | c | d | e | q | x | y | z |
---------------------------------------
| 1 | 2 | 3 | - | - | - | - | - | - |
| 3 | 4 | - | 5 | 5 | - | - | - | - |
| 2 | - | - | - | - | 5 | 1 | 2 | 3 | 
4

1 回答 1

2

请将此作为扩展评论而不是答案。

在熊猫只有你有

import pandas as pd

dataset = [
    dict(a = 1, b = 2, c = 3),
    dict(a = 3, b = 4, d = 5, e = 5),
    dict(a = 2, x = 1, y = 2, z = 3, q = 5)
]

df = pd.DataFrame(dataset)

并且df

   a    b    c    d    e    x    y    z    q
0  1  2.0  3.0  NaN  NaN  NaN  NaN  NaN  NaN
1  3  4.0  NaN  5.0  5.0  NaN  NaN  NaN  NaN
2  2  NaN  NaN  NaN  NaN  1.0  2.0  3.0  5.0

但是,如果你搬到 dask 我发现的唯一可能的解决方案是以下

import pandas as pd
import dask.dataframe as dd
from dask import delayed, compute

dataset = [
    dict(a = 1, b = 2, c = 3),
    dict(a = 3, b = 4, d = 5, e = 5),
    dict(a = 2, x = 1, y = 2, z = 3, q = 5)
]

def fun(d):
    return pd.DataFrame(d, index=[0])

lst = [delayed(fun)(l) for l in dataset]

df = dd.concat(compute(lst)[0])

在我看来,这根本没有效率。看看是否有适当的方法来获得相同的输出将会很有趣。

于 2020-07-16T22:34:26.860 回答