我正在尝试垂直连接两个Dask DataFrames
我有以下Dask DataFrame:
d = [
['A','B','C','D','E','F'],
[1, 4, 8, 1, 3, 5],
[6, 6, 2, 2, 0, 0],
[9, 4, 5, 0, 6, 35],
[0, 1, 7, 10, 9, 4],
[0, 7, 2, 6, 1, 2]
]
df = pd.DataFrame(d[1:], columns=d[0])
ddf = dd.from_pandas(df, npartitions=5)这是作为Pandas DataFrame的数据
A B C D E F
0 1 4 8 1 3 5
1 6 6 2 2 0 0
2 9 4 5 0 6 35
3 0 1 7 10 9 4
4 0 7 2 6 1 2这是Dask DataFrame
Dask DataFrame Structure:
A B C D E F
npartitions=4
0 int64 int64 int64 int64 int64 int64
1 ... ... ... ... ... ...
2 ... ... ... ... ... ...
3 ... ... ... ... ... ...
4 ... ... ... ... ... ...
Dask Name: from_pandas, 4 tasks我正在尝试垂直连接2个Dask DataFrames:
ddf_i = ddf + 11.5
dd.concat([ddf,ddf_i],axis=0)但是我得到了这个错误:
Traceback (most recent call last):
...
File "...", line 572, in concat
raise ValueError('All inputs have known divisions which cannot '
ValueError: All inputs have known divisions which cannot be concatenated
in order. Specify interleave_partitions=True to ignore order但是,如果我尝试:
dd.concat([ddf,ddf_i],axis=0,interleave_partitions=True)然后它看起来是有效的。将其设置为True (就性能-速度而言)有问题吗?或者还有其他方法可以垂直连接2个Dask DataFrames吗?
发布于 2017-05-06 05:04:33
如果您检查数据帧ddf.divisions的分区,您会发现,假设有一个分区,它在那里有索引的边缘:(0,4)。这对dask很有用,因为它知道您何时对数据执行某些操作,而不是使用不包含所需索引值的分区。这也是为什么当索引适合于作业时,某些dask操作会快得多。
连接时,第二个数据帧与第一个数据帧具有相同的索引。如果索引值在两个分区中具有不同的范围,则连接将在不交错的情况下工作。
发布于 2021-09-18 18:26:39
穆杜兰特的答案是正确的,这个答案使用Dask v2021.08.1用MCVE代码片段进行了详细阐述。示例使理解划分和交错变得更容易。
垂直连接DataFrames
创建两个DataFrames,将它们连接起来,然后查看结果。
df = pd.DataFrame(
{"nums": [1, 2, 3, 4, 5, 6], "letters": ["a", "b", "c", "d", "e", "f"]}
)
ddf1 = dd.from_pandas(df, npartitions=2)
df = pd.DataFrame({"nums": [88, 99], "letters": ["xx", "yy"]})
ddf2 = dd.from_pandas(df, npartitions=1)
ddf3 = dd.concat([ddf1, ddf2])
print(ddf3.compute())
nums letters
0 1 a
1 2 b
2 3 c
3 4 d
4 5 e
5 6 f
0 88 xx
1 99 yy垂直连接时划分元数据
创建两个DataFrames,将它们连接起来,并说明有时此操作会导致部门元数据丢失。
def print_partitions(ddf):
for i in range(ddf.npartitions):
print(ddf.partitions[i].compute())
df = pd.DataFrame(
{"nums": [1, 2, 3, 4, 5, 6], "letters": ["a", "b", "c", "d", "e", "f"]}
)
ddf1 = dd.from_pandas(df, npartitions=2)
ddf1.divisions # (0, 3, 5)
df = pd.DataFrame({"nums": [88, 99], "letters": ["xx", "yy"]})
ddf2 = dd.from_pandas(df, npartitions=1)
ddf2.divisions # (0, 1)
ddf3 = dd.concat([ddf1, ddf2])
ddf3.divisions # (None, None, None, None)设置interleave_partitions=True以避免丢失分区元数据。
ddf3_interleave = dd.concat([ddf1, ddf2], interleave_partitions=True)
ddf3_interleave.divisions # (0, 1, 3, 5)不需要交错时的
创建两个没有重叠分区的DataFrames,将它们连接起来,并确认分区元数据没有丢失:
df = pd.DataFrame(
{"nums": [1, 2, 3, 4], "letters": ["a", "b", "c", "d"], "some_index": [4, 5, 6, 7]}
)
ddf1 = dd.from_pandas(df, npartitions=2)
ddf1 = ddf1.set_index("some_index")
df = pd.DataFrame({"nums": [88, 99], "letters": ["xx", "yy"], "some_index": [10, 20]})
ddf2 = dd.from_pandas(df, npartitions=1)
ddf2 = ddf2.set_index("some_index")
ddf3 = dd.concat([ddf1, ddf2])
ddf3.divisions # (4, 6, 10, 20)我写了一篇博客文章来更详细地解释这一点。如果你喜欢这个链接,请让我知道。
https://stackoverflow.com/questions/43810905
复制相似问题