![](/img/trans.png)
[英]How to find cells with None values (string data-type) and replace them with Null in Spark DataFrame?
[英]How to count No. of cells with None value (string data-type) in all columns of a Spark DataFrame?
我有一個大數據集,有些列具有 String 數據類型。 由於拼寫錯誤,一些單元格沒有值,但寫成不同的 styles(小寫或大寫字母,有或沒有空格,有或沒有括號等)。
我想計算所有列中所有這些值(不包括 Null 值)的編號。 示例數據集如下:
data = [("A", "None", 1), \
("A", "(None)", 2), \
("[None", "none", 3), \
("(none]", "[None]", 4), \
("A", "(none)", 5), \
("A", "(none", 6), \
("A", "none ", 7), \
(" NOne ", None, None), \
]
# Create DataFrame
columns= ["col_1", "col_2", "Number"]
df = spark.createDataFrame(data = data, schema = columns)
預期結果是:
{'col_1': 3, 'col_2': 7, 'Number': 0}
知道如何在 PySpark 之前做到這一點嗎?
邏輯是:
count_result = {}
for c in df.columns:
count_result[c] = df.select(c).filter(F.lower(F.trim(F.regexp_replace(c, r"(?:^\[|^\(|^\<|^\{|\]$|\)$|\>$|\}$)", ""))) == "none") \
.count()
print(count_result)
Output:
{'col_1': 3, 'col_2': 7, 'Number': 0}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.