python - Dask - 查找重复值
问题描述
我需要在dask
DataFrame 的列中查找重复项。
因为pandas
这是有duplicated()
方法的。虽然dask
不支持它。
问:在 dask 中获取所有重复值的最佳方法是什么?
我的想法:
创建一个我正在检查的列作为索引,drop_duplicates
然后join
.
有没有更好的解决方案?
例如:
df = pandas.DataFrame(
[
['a'],
['b'],
['c'],
['a']
],
columns=['col']
)
df_test = dask.dataframe.from_pandas(df, npartitions=2)
# Expected to get dataframe with value 'a', as it appears twice
解决方案
我想出了以下解决方案:
import dask.dataframe as dd
import pandas
if __name__ == '__main__':
df = pandas.DataFrame(
[
['a'],
['b'],
['c'],
['a']
],
columns=["col-a"]
)
ddf = dd.from_pandas(df, npartitions=2)
# Apparently the code below will fail if the dask DataFrame is empty
if ddf.index.size.compute() != 0:
# With indexing data will be repartitioned - and all duplicated can be found within one partition
indexed_df = ddf.set_index('col-a', drop=False)
# Mark duplicate values within partitions. dask DataFrame does not support duplicates().
dups = indexed_df.map_partitions(lambda d: d.duplicated())
# Get duplicated by indexes calculated in previous step.
duplicates = indexed_df[dups].compute().index.tolist()
print(duplicates) # Prints: ['a']
这可以进一步改善吗?
推荐阅读
- visual-studio - 在现有的 IIF 语句中添加附加条件 - SSRS
- angular - 如何为不同的用户显示不同的仪表板
- powershell - 使用 PowerShell 更改下拉选择
- eclipse - 在eclipse中创建动态webapp
- html - 如何从我的 custom.css 文件中应用 btn btn-primary 引导类?
- javascript - 如何在 Laravel5 中禁用选择?
- html - 为什么“宽度:-moz-fit-content”对输入 [type=text] 不起作用?
- c# - 如何在解析文件中的两个不同对象时避免重复代码?
- html - 如何在图像和文本之间创建一条水平线
- mysql - 如何在不使用 Sequelize 的情况下定义模型?(带有 MySQL 的 Node.JS)