Python Dask数据帧按分区分组

Python Dask数据帧按分区分组,python,pandas,dask,Python,Pandas,Dask,我有一些相当大的csv文件(~10gb),希望利用dask进行分析。但是,根据我设置dask对象读入的分区数,我的groupby结果会发生变化。我的理解是,dask利用分区来获得核心外处理的好处,但它仍然会返回适当的groupby输出。情况似乎并非如此,我正在努力找出哪些备用设置是必要的。下面是一个小例子: df = pd.DataFrame({'A': np.arange(100), 'B': np.random.randn(100), 'C': np.random.randn(100), '

我有一些相当大的csv文件(~10gb),希望利用dask进行分析。但是,根据我设置dask对象读入的分区数,我的groupby结果会发生变化。我的理解是,dask利用分区来获得核心外处理的好处,但它仍然会返回适当的groupby输出。情况似乎并非如此,我正在努力找出哪些备用设置是必要的。下面是一个小例子:

df = pd.DataFrame({'A': np.arange(100), 'B': np.random.randn(100), 'C': np.random.randn(100), 'Grp1': np.repeat([1, 2], 50), 'Grp2': [3, 4, 5, 6], 25)})

test_dd1 = dd.from_pandas(df, npartitions=1)
test_dd2 = dd.from_pandas(df, npartitions=2)
test_dd5 = dd.from_pandas(df, npartitions=5)
test_dd10 = dd.from_pandas(df, npartitions=10)
test_dd100 = dd.from_pandas(df, npartitions=100)

def test_func(x):
    x['New_Col'] = len(x[x['B'] > 0.]) / len(x['B'])
    return x

test_dd1.groupby(['Grp1', 'Grp2']).apply(test_func).compute().head()
   A               B               C Grp1 Grp2 New_Col
0  0 -0.561376 -1.422286     1     3     0.48
1  1 -1.107799  1.075471     1     3     0.48
2  2 -0.719420 -0.574381     1     3     0.48
3  3 -1.287547 -0.749218     1     3     0.48
4  4  0.677617 -0.908667     1     3     0.48

test_dd2.groupby(['Grp1', 'Grp2']).apply(test_func).compute().head()
   A               B              C  Grp1 Grp2 New_Col
0  0 -0.561376 -1.422286     1     3     0.48
1  1 -1.107799  1.075471     1     3     0.48
2  2 -0.719420 -0.574381     1     3     0.48
3  3 -1.287547 -0.749218     1     3     0.48
4  4  0.677617 -0.908667     1     3     0.48

test_dd5.groupby(['Grp1', 'Grp2']).apply(test_func).compute().head()
   A               B              C  Grp1 Grp2 New_Col
0  0 -0.561376 -1.422286     1     3     0.45
1  1 -1.107799  1.075471     1     3     0.45
2  2 -0.719420 -0.574381     1     3     0.45
3  3 -1.287547 -0.749218     1     3     0.45
4  4  0.677617 -0.908667     1     3     0.45

test_dd10.groupby(['Grp1', 'Grp2']).apply(test_func).compute().head()
   A               B              C  Grp1 Grp2 New_Col
0  0 -0.561376 -1.422286     1     3      0.5
1  1 -1.107799  1.075471     1     3      0.5
2  2 -0.719420 -0.574381     1     3      0.5
3  3 -1.287547 -0.749218     1     3      0.5
4  4  0.677617 -0.908667     1     3      0.5

test_dd100.groupby(['Grp1', 'Grp2']).apply(test_func).compute().head()
   A               B              C  Grp1 Grp2  New_Col
0  0 -0.561376 -1.422286     1     3        0
1  1 -1.107799  1.075471     1     3        0
2  2 -0.719420 -0.574381     1     3        0
3  3 -1.287547 -0.749218     1     3        0
4  4  0.677617 -0.908667     1     3        1

df.groupby(['Grp1', 'Grp2']).apply(test_func).head()
   A               B               C Grp1 Grp2 New_Col
0  0 -0.561376 -1.422286     1     3     0.48
1  1 -1.107799  1.075471     1     3     0.48
2  2 -0.719420 -0.574381     1     3     0.48
3  3 -1.287547 -0.749218     1     3     0.48
4  4  0.677617 -0.908667     1     3     0.48
groupby步骤是否只在每个分区内运行,而不是查看整个数据帧?在这种情况下,将npartitions设置为1并不重要,而且似乎对性能影响不大,但由于read_csv会自动设置一定数量的分区,如何设置调用以确保groupby结果准确


谢谢

我对这个结果感到惊讶。Groupby.apply应该返回相同的结果,而不管分区的数量如何。如果您能提供一个可复制的示例,我鼓励您,其中一位开发人员会看一看。

我的第一个想法是,dask的groupby/apply可能无法保证结果的顺序,但它们可能都在那里。是的,我也这么想,但我做了各种独特的切片,随着分区数的增加,组内的结果最终会有所不同。例如,在一组唯一的“grp1/grp2”中会有两个不同的值。这个问题有什么解决方案吗?这对我来说是一个阻碍。我将继续为group by使用自行开发的解决方案,直到对此问题有了解释或解决方法。我找不到提出的问题,因为唯一的解决方法是向操作系统建议。解决的问题在这里:我已经测试了操作系统,但它并没有为所有分区返回相同的结果。也许这种意外的行为再次发生是因为?