[英]PySpark search inside very large dataframe
我在 pyspark 中有一个非常大的数据框。 它有超过 1000 万行和 30 多列。
在整个数据帧中搜索给定值列表并删除包含该值的行的最佳和有效方法是什么?
The given list of values: list=['1097192','10727550','1098754'] The dataframe(df) is : +---------+--------------+---------------+---------+------------+ | id | first_name | last_name | Salary | Verifycode | +---------+--------------+---------------+---------+------------+ | 1986 | Rollie | Lewin | 1097192 | 42254172 | -Remove Row | 289743 | Karil | Sudron | 2785190 | 3703538 | | 3864 | Massimiliano | Dallicott | 1194553 | 23292573 | | 49074 | Gerry | Grinnov | 1506584 | 62291161 | | 5087654 | Nat | Leatherborrow | 1781870 | 55183252 | | 689 | Thaine | Tipple | 2150105 | 40583249 | | 7907 | Myrlene | Croley | 2883250 | 70380540 | | 887 | Nada | Redier | 2676139 | 10727550 | -Remove Row | 96533 | Sonny | Bosden | 1050067 | 13110714 | | 1098754 | Dennie | McGahy | 1804487 | 927935 | -Remove Row +---------+--------------+---------------+---------+------------+
如果它是一个较小的数据帧,我可以使用 collect() 或 toLocalIterator() 函数,然后遍历行并根据列表值将其删除。
由于它是一个非常大的数据框,因此解决此问题的最佳方法是什么?
我现在想出了这个解决方案,但有没有更好的方法:
column_names = df.schema.names for name in column_names: df = df.filter(~col(name).isin(list))
你有使用过滤数据帧的正确的做法filter
和isin
功能。 如果列表很小(几千而不是几百万),您可以使用isin
函数。 还要确保您的数据帧分区为至少 3*数量的执行器上的 CPU。 必须有很多分区,否则并行性会受到影响。
我对 Scala 很满意,所以请从下面的代码中获取概念。 您需要通过组合所有要过滤的列来构建一个Column
对象。 然后在dataframe.filter
上提供最后一个列对象
column_names = df.schema.names
colFinal // initialize with 1 column name as col("colName").isin(list)
for name in column_names:
colFinal = colFinal.or(col(name).isin(list))
df = df.filter(!colFinal) // apply negation of final column object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.