載入Excel檔案,並以 DataFrame 回傳結果。
語法
excel(path, dataAddress=None, headerRows=None, listSheets=None,
dateFormat=None, timestampFormat=None)
參數
| 參數 | 類型 | 說明 |
|---|---|---|
path |
力量或列表 | 一條或多條輸入路徑。 |
dataAddress |
力量,選用 | Excel 檔案中資料的位址。 |
headerRows |
智力或力量,選擇性 | 標頭列的數量。 |
listSheets |
Bool 或 STR,選擇性 | 若 True,則回傳紙張名稱列表,而非讀取資料。 |
dateFormat |
力量,選用 | 日期格式字串。 |
timestampFormat |
力量,選用 | 時間戳格式字串。 |
退貨
DataFrame
Examples
把一個 DataFrame 寫進 Excel 檔案,然後讀回去。
import tempfile
with tempfile.TemporaryDirectory(prefix="excel") as d:
spark.createDataFrame(
[{"age": 100.1, "name": "Alice"}]
).write.mode("overwrite").option("headerRows", 1).excel(d)
spark.read.excel(d, headerRows=1).show()
# +-----+------------+
# | age| name|
# +-----+------------+
# |100.1|Alice|
# +-----+------------+