簡體   English   中英

在 PySpark 中檢查字典中鍵的值是否為空

[英]Checking if the value of a key in a dictionary is empty in PySpark

我對 Spark 很陌生,但我還沒有弄清楚這一點。 我有一個字典列表:

list_d =  [ 

{"asset": "Anna",
 "grade": "somestring",
 "obs": "True"
},
{"asset": "Bob",
 "grade": "somestrings",
 "obs": "False"
},
{"asset": "",
 "grade": "somestrings",
 "obs": "False"
}
]

僅當"asset"不為空時,如何編寫 if-else 條件以便我做某事(在我的情況下,添加一列,但這並不重要)? 我嘗試了以下但我沒有得到任何東西:

asset_class = table["asset"]
if asset_class:
    # df = df.withColumn("asset", ...) or anything

我只想檢查該值是否存在。 我怎樣才能做到這一點?

檢查此代碼段。

list_d = [
    {"asset": "Anna",
     "grade": "somestring",
     "obs": "True"
     },
    {"asset": "Bob",
     "grade": "somestrings",
     "obs": "False"
     },
    {"asset": "",
     "grade": "somestrings",
     "obs": "False"
     }
]

for table in list_d:
    asset_class = table.get("asset", None)
    if asset_class != None and asset_class != "":
        # Add your logic here
        print(table)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM