如何在 PySpark 資料框中檢查子字串?


Pyspark 是提供 Apache Spark 介面的庫。Apache Spark 是用於處理大型資料集的分散式計算系統。除了傳統的 Java 和 Scala 語言之外,我們還可以使用 Python 在 Pyspark 中編寫 Spark 應用程式。

在 PySpark 中驗證子字串

Pyspark 提供了資料框 API,它可以幫助我們操作結構化資料,例如 SQL 查詢。它可以讀取各種資料格式,如 parquet、csv、JSON 等。它還提供支援機器學習庫的功能,可以使用分類、迴歸、聚類等。

我們有不同的方法來檢查 PySpark 資料框中的子字串。在本文中,我們將討論以下內容:

  • 使用 like() 或 rlike()

  • 使用 substr()

安裝 PySpark

在詳細瞭解每種方法之前,我們必須使用以下程式碼在我們的工作環境中安裝 pyspark。

!pip install pyspark

安裝成功後,我們將得到如下所示的類似輸出:

Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Collecting pyspark
  Downloading pyspark-3.3.2.tar.gz (281.4 MB)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Installing collected packages: py4j, pyspark
  Attempting uninstall: py4j
    Found existing installation: py4j 0.10.9.7
    Uninstalling py4j-0.10.9.7:
      Successfully uninstalled py4j-0.10.9.7
Successfully installed py4j-0.10.9.5 pyspark-3.3.2

在 PySpark 中建立資料框

現在,我們必須使用指定的資料詳細資訊建立 PySpark 的資料框。步驟如下:

  • 我們必須匯入所有必要的 PySpark 模組和包才能建立資料框。

from pyspark.sql import SparkSession
from pyspark.sql.types import *
  • 接下來,我們必須建立 sparksession 來使用 pyspark 資料框。

spark = SparkSession.builder \
    .appName("substring check") \
    .getOrCreate()
  • 我們必須使用 pyspark 定義用於建立資料框的模式。

schema = StructType([
    StructField("name", StringType(), True),
    StructField("age", IntegerType(), True),
    StructField("gender", StringType(), True),
    StructField("occupation", StringType(), True),
    StructField("salary", DoubleType(), True)
])
  • 在此步驟中,我們將建立列表行,然後形成資料框。

data = [("John", 25, "Male", "Developer", 5000.0),
        ("Jane", 30, "Female", "Manager", 7000.0),
        ("Bob", 35, "Male", "Director", 10000.0),
        ("Alice", 40, "Female", "CEO", 15000.0)]
  • 我們根據我們在上述兩個步驟中指定的模式和資料建立資料框,並在建立後顯示資料框。

df = spark.createDataFrame(data, schema)
df.show()

輸出

以下是使用pyspark建立的資料框的輸出。

------+---+------+----------+-------+
| name|age|gender|occupation| salary|
+-----+---+------+----------+-------+
| John| 25|  Male| Developer| 5000.0|
| Jane| 30|Female|   Manager| 7000.0|
|  Bob| 35|  Male|  Director|10000.0|
|Alice| 40|Female|       CEO|15000.0|
+-----+---+------+----------+-------+

現在,讓我們看看在 PySpark 資料框中檢查子字串的每種方法。

使用 like() 或 rlike() 函式

在 PySpark 中,我們有兩個函式like()rlike();它們用於檢查資料框的子字串。like()函式用於檢查任何特定列是否包含指定的模式,而rlike()函式則檢查列中是否存在正則表示式模式。

from pyspark.sql.functions import col
df = spark.createDataFrame(data, schema)
df.show()
df.where(col("occupation").like("%Developer%")).show()
df.where(col("occupation").rlike(".*Developer.*")).show()

輸出

like 和 rlike 函式的輸出如下所示。

+-----+---+------+----------+-------+
| name|age|gender|occupation| salary|
+-----+---+------+----------+-------+
| John| 25|  Male| Developer| 5000.0|
| Jane| 30|Female|   Manager| 7000.0|
|  Bob| 35|  Male|  Director|10000.0|
|Alice| 40|Female|       CEO|15000.0|
+-----+---+------+----------+-------+

+----+---+------+----------+------+
|name|age|gender|occupation|salary|
+----+---+------+----------+------+
|John| 25|  Male| Developer|5000.0|
+----+---+------+----------+------+

+----+---+------+----------+------+
|name|age|gender|occupation|salary|
+----+---+------+----------+------+
|John| 25|  Male| Developer|5000.0|
+----+---+------+----------+------+

使用 substr() 函式

substr函式用於從列中提取子字串,然後檢查子字串是否與指定的模式匹配。

from pyspark.sql.functions import col
df = spark.createDataFrame(data, schema)
df.show()
df.where(col("occupation").substr(1, 5) == "Manag").show()

輸出

pysparksubstr函式的輸出如下所示。

+-----+---+------+----------+-------+
| name|age|gender|occupation| salary|
+-----+---+------+----------+-------+
| John| 25|  Male| Developer| 5000.0|
| Jane| 30|Female|   Manager| 7000.0|
|  Bob| 35|  Male|  Director|10000.0|
|Alice| 40|Female|       CEO|15000.0|
+-----+---+------+----------+-------+

+----+---+------+----------+------+
|name|age|gender|occupation|salary|
+----+---+------+----------+------+
|Jane| 30|Female|   Manager|7000.0|
+----+---+------+----------+------+

更新於:2023年8月9日

3K+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.