我正在尝试使用PySpark在spark数据帧的所有列中查找一个子字符串。我目前知道如何使用filter通过一列搜索子字符串,并且包含:
df.filter(df.col_name.contains('substring'))如何扩展这条语句或利用另一条语句在多个列中搜索子字符串匹配?
发布于 2019-10-17 04:47:43
您可以一下子将语句概括为过滤器:
from pyspark.sql.functions import col, count, when
# Converts all unmatched filters to NULL and drops them.
df = df.select([when(col(c).contains('substring'), col(c)).alias(c) for c in df.columns]).na.drop()或
您可以简单地循环遍历这些列,并应用相同的过滤器:
for col in df.columns:
df = df.filter(df[col].contains("substring"))https://stackoverflow.com/questions/58420839
复制相似问题