I’m trying to export pandas df to xlsx file in dbfs.
Understand that the traditional df.to_excel() is not supported in databricks.
`from pyspark.sql.functions import *
from pyspark.sql.types import *
from io import BytesIO
from databricks import dbutils
spark_df = spark.createDataFrame(df)
final = "/dbfs/folder/filename_{this_week_mon}.xlsx"
with BytesIO() as buffer:
spark_df.toPandas().to_excel(buffer, index=False)
buffer.seek(0) # Reset the position to the beginning of the buffer`
I tried something like this but I am getting an error: ImportError: cannot import name ‘dbutils’ from ‘databricks’ (/databricks/python_shell/databricks/init.py). I tried adding dbutils and databricks to the library manually but it still didn’t work.
I also couldn’t use SparkSession for the same reason. from pyspark.sql import SparkSession
didn’t work after multiple attempts to add it to the library.