[英]Dask DataFrame: apply custom function to the entire Column, involving min(), max()
一个包含 100M 记录和 60K 列的巨大数据集加载到 Dask dataframe 中。 需要对整个列执行 min() & max()。 由于 memory 问题,已排除使用 Pandas。
#Sample Dask Dataframe
import dask.dataframe as dd
df = pd.DataFrame({'col1': [1, 2, 3, 4, 5],
'col2': [2., 3., 4., 5., 6.],
'col3': [4, 6, 8, 3, 2],
.
.
.
'col60000':[3,4,5,6,7]
})
ddf = dd.from_pandas(df, npartitions=30)
我无法使用 map_partitions function 因为它适用于相应的分区而不是整个列
min_deviation = lambda x: (x - x.min())
for col in ddf.columns:
print("processing column:", col)
res = ddf[col].map_partitions(min_deviation).compute()
print(res)
Results:
processing column: col1
0 0
1 1
2 2
3 0
4 1
Name: col1, dtype: int64
processing column: col2
0 0.0
1 1.0
2 2.0
3 0.0
4 1.0
Name: col2, dtype: float64
processing column: col3
0 0
1 2
2 4
3 1
4 0
Name: col3, dtype: int64
此外,按列操作不支持 Dask apply() function。
有没有其他方法可以使用 Dask dataframe 执行整个列操作。
dask dataframe 具有默认按列工作的max
和min
方法,并从整个数据、所有分区产生结果。 您还可以将这些结果用于进一步的算术运算,无论是否将它们计算为具体值
df.min().compute()
- 每列的具体最小值(df - df.min())
- 你所说的懒惰版本(df - df.min().compute())
- 预先计算最小值(可能有用,取决于您接下来打算做什么)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.