我有一個包含多列和多行的 Delta 資料框。
我做了以下事情:
Delta.limit(1).select("IdEpisode").show()
---------
|IdEpisode|
---------
| 287 860|
---------

但是,當我這樣做時:
Delta.filter("IdEpisode == '287 860'").show()

它回傳 0 行,這很奇怪,因為我們可以清楚地看到Id資料幀中的存在。
我認為它是關于' '中間的,但我不明白為什么會出現問題以及如何解決它。
重要編輯:
Doing Delta.limit(1).select("IdEpisode").collect()[0][0]
回: '287\xa0860'
然后做:
Delta.filter("IdEpisode == '287\xa0860'").show()
回傳了我一直在尋找的行。有什么解釋嗎?
uj5u.com熱心網友回復:
此字符稱為NO-BREAK SPACE。這不是常規空間,這就是它與您的過濾不匹配的原因。
您可以regexp_replace在應用過濾器之前使用函式將其洗掉:
import pyspark.sql.functions as F
Delta = spark.createDataFrame([('287\xa0860',)], ['IdEpisode'])
# replace NBSP character with normal space in column
Delta = Delta.withColumn("IdEpisode", F.regexp_replace("IdEpisode", '[\\u00A0]', ' '))
Delta.filter("IdEpisode = '287 860'").show()
# ---------
#|IdEpisode|
# ---------
#| 287 860|
# ---------
您還可以通過使用正則運算式\p{Z}將所有型別的空格替換為常規空格來清理列:
\p{Z}或\p{Separator}:任何型別的空格或不可見分隔符。
Delta = Delta.withColumn("IdEpisode", F.regexp_replace("IdEpisode", '\\p{Z}', ' '))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/400125.html
標籤:阿帕奇火花 火花 apache-spark-sql 过滤
