我有一個具有以下架構的火花資料框:
stat_chiamate
|
chiamate_ricevute: struct (nullable = true)
| | |-- h_0: string (nullable = true)
| | |-- h_1: string (nullable = true)
| | |-- h_10: string (nullable = true)
| | |-- h_11: string (nullable = true)
| | |-- h_12: string (nullable = true)
| | |-- h_13: string (nullable = true)
| | |-- h_14: string (nullable = true)
| | |-- h_15: string (nullable = true)
| | |-- h_16: string (nullable = true)
| | |-- h_17: string (nullable = true)
| | |-- h_18: string (nullable = true)
| | |-- h_19: string (nullable = true)
| | |-- h_2: string (nullable = true)
| | |-- h_20: string (nullable = true)
| | |-- h_21: string (nullable = true)
| | |-- h_22: string (nullable = true)
| | |-- h_23: string (nullable = true)
| | |-- h_3: string (nullable = true)
| | |-- h_4: string (nullable = true)
| | |-- h_5: string (nullable = true)
| | |-- h_6: string (nullable = true)
| | |-- h_7: string (nullable = true)
| | |-- h_8: string (nullable = true)
| | |-- h_9: string (nullable = true)
| | |-- n_totale: string (nullable = true)
我想要一個資料框,如:
stat_chiamate: struct (nullable = true)
|
chiamate_ricevute: Array
|-- element(String)
其中chiamate_ricevute是欄位值串列,例如:
h_0= 0
h_1= 1
h_2= 2
.
.
.
h_23=23
n_totale=412
我想要:
[0,1,2....,23] <-- I don't want n_totale values
在我的代碼中,我使用df.select("stat_chiamate.chiamate_ricevute.*").schema.fieldNames()[:-1]但我只有一個,fieldsName但我如何使用它們?
df=df.select(F.array(*[field for field in
df.select("stat_chiamate.chiamate_ricevute.*").schema.fieldNames() if field.startswith("h_")]).alias("CIRCO"))
uj5u.com熱心網友回復:
您可以使用資料框的架構,特別是您的架構struct來提取所有欄位名稱n_totale,然后將它們包裝成一個陣列。
from pyspark.sql import functions as f
fields = ['chiamate_ricevute.' field.name for field in df.schema[0].dataType
if field.name != 'n_totale']
result = df.select(f.array(fields).alias("chiamate_ricevute"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/347528.html
上一篇:Pyspark:最常用的詞
