2 回答

TA貢獻1820條經驗 獲得超2個贊
如果您想使用包pandas從 Azure blob 讀取 CSV 文件,對其進行處理并將此 CSV 文件寫入 Azure Databricks 中的 Azure blob,我建議您將 Azure blob 存儲掛載為 Databricks 文件系統,然后執行此操作。欲了解更多詳情,請參閱此處。
例如
裝載 Azure 斑點
dbutils.fs.mount(
source = "wasbs://<container-name>@<storage-account-name>.blob.core.windows.net",
mount_point = "/mnt/<mount-name>",
extra_configs = {"fs.azure.account.key.<storage-account-name>.blob.core.windows.net":"<account access key>"})
處理 csv
import os
import glob
import pandas as pd
os.chdir(r'/dbfs/mnt/<mount-name>/<>')
allFiles = glob.glob("*.csv") # match your csvs
for file in allFiles:
print(f" The old content of file {file} : ")
df= pd.read_csv(file, header=None)
print(df)
df = df.iloc[4:,]
df.to_csv(file, index=False,header=False)
print(f" The new content of file {file} : ")
df= pd.read_csv(file,header=None)
print(df)
break

TA貢獻1812條經驗 獲得超5個贊
A,替代方法是將 dbfs 文件掛載為 Spark 數據幀,然后將其從 Sparkdf 轉換為 pandas df:
# mount blob storage
spark.conf.set("fs.azure.account.key.storageaccountname.blob.core.windows.net",
"storageaccesskey")
dfspark = spark.read.csv("wasbs://[email protected]
/filename.csv", header="true")
# convert from sparkdf to pandasdf
df = dfspark.toPandas()
添加回答
舉報