У меня есть большой фрейм данных Dask (4 Гб), который выглядит следующим образом:
title text filename
0 xx xx blah xx_xx.txt
1 xx xx blah b.txt
2 yyyyy bing c.txt
Я бы хотел сгруппировать по title
, text
и |
, отделяя filename
. Если бы я мог дополнительно определить filename
, который ближе всего к title
, это также было бы здорово. Это должно дать мне:
title text filename best_match
0 xx xx blah xx_xx.txt|b.txt xx_xx.txt
1 yyyyy bing c.txt c.txt
Я пробовал это с Pandas (так как у меня 32 ГБ памяти), используя:
dfg = df.groupby (['title', 'text' ]). filename.agg ([('filename', '|' .join)]). reset_index ()
, но это дало мне эту ошибку (я на Windows 10, поэтому я предполагаю, что это почему это отстой):
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\User\AppData\Roaming\Python\Python37\site-packages\pandas\core\groupby\generic.py", line 851, in aggregate
ret = self._aggregate_multiple_funcs(func_or_funcs, (_level or 0) + 1)
File "C:\Users\User\AppData\Roaming\Python\Python37\site-packages\pandas\core\groupby\generic.py", line 930, in _aggregate_multiple_funcs
results[name] = obj.aggregate(func)
File "C:\Users\User\AppData\Roaming\Python\Python37\site-packages\pandas\core\groupby\generic.py", line 860, in aggregate
return self._python_agg_general(func_or_funcs, *args, **kwargs)
File "C:\Users\User\AppData\Roaming\Python\Python37\site-packages\pandas\core\groupby\groupby.py", line 920, in _python_agg_general
return self._wrap_aggregated_output(output)
File "C:\Users\User\AppData\Roaming\Python\Python37\site-packages\pandas\core\groupby\generic.py", line 955, in _wrap_aggregated_output
return self._reindex_output(result)._convert(datetime=True)
File "C:\Users\User\AppData\Roaming\Python\Python37\site-packages\pandas\core\groupby\groupby.py", line 2471, in _reindex_output
levels_list, names=self.grouper.names
File "C:\Users\User\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexes\multi.py", line 539, in from_product
codes = cartesian_product(codes)
File "C:\Users\User\AppData\Roaming\Python\Python37\site-packages\pandas\core\reshape\util.py", line 58, in cartesian_product
for i, x in enumerate(X)
File "C:\Users\User\AppData\Roaming\Python\Python37\site-packages\pandas\core\reshape\util.py", line 58, in <listcomp>
for i, x in enumerate(X)
File "<__array_function__ internals>", line 6, in repeat
File "E:\WPy-3710\python-3.7.1.amd64\lib\site-packages\numpy\core\fromnumeric.py", line 482, in repeat
return _wrapfunc(a, 'repeat', repeats, axis=axis)
File "E:\WPy-3710\python-3.7.1.amd64\lib\site-packages\numpy\core\fromnumeric.py", line 61, in _wrapfunc
return bound(*args, **kwds)
MemoryError: Unable to allocate 111. TiB for an array with shape (30553687409271,) and data type int32
Попытка сделать то же самое с помощью Dask:
dfg = df.groupby(['title', 'text']).filename.agg([('filename', ':'.join)]).reset_index().compute()
По какой-то причине дает мне:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "E:\WPy-3710\python-3.7.1.amd64\lib\site-packages\dask\dataframe\groupby.py", line 1809, in agg
return self.aggregate(arg, split_every=split_every, split_out=split_out)
File "E:\WPy-3710\python-3.7.1.amd64\lib\site-packages\dask\dataframe\groupby.py", line 1797, in aggregate
arg, split_every=split_every, split_out=split_out
File "E:\WPy-3710\python-3.7.1.amd64\lib\site-packages\dask\dataframe\groupby.py", line 1496, in aggregate
chunk_funcs, aggregate_funcs, finalizers = _build_agg_args(spec)
File "E:\WPy-3710\python-3.7.1.amd64\lib\site-packages\dask\dataframe\groupby.py", line 698, in _build_agg_args
impls = _build_agg_args_single(result_column, func, input_column)
File "E:\WPy-3710\python-3.7.1.amd64\lib\site-packages\dask\dataframe\groupby.py", line 742, in _build_agg_args_single
raise ValueError("unknown aggregate {}".format(func))
ValueError: unknown aggregate ('filename', <built-in method join of str object a
Попытался вынуть имя файла:
dfg = df.groupby(['title', 'text']).agg([('filename', ':'.join)]).reset_index().compute()
но это дало мне:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "E:\WPy-3710\python-3.7.1.amd64\lib\site-packages\dask\dataframe\groupby.py", line 1742, in agg
return self.aggregate(arg, split_every=split_every, split_out=split_out)
File "E:\WPy-3710\python-3.7.1.amd64\lib\site-packages\dask\dataframe\groupby.py", line 1737, in aggregate
arg, split_every=split_every, split_out=split_out
File "E:\WPy-3710\python-3.7.1.amd64\lib\site-packages\dask\dataframe\groupby.py", line 1496, in aggregate
chunk_funcs, aggregate_funcs, finalizers = _build_agg_args(spec)
File "E:\WPy-3710\python-3.7.1.amd64\lib\site-packages\dask\dataframe\groupby.py", line 698, in _build_agg_args
impls = _build_agg_args_single(result_column, func, input_column)
File "E:\WPy-3710\python-3.7.1.amd64\lib\site-packages\dask\dataframe\groupby.py", line 742, in _build_agg_args_single
raise ValueError("unknown aggregate {}".format(func))
ValueError: unknown aggregate ('filename', <built-in method join of str object a
Как мне достичь своей цели в Даске или есть способ заставить Pandas сделать это?