Reading a xlsx file with PySpark

Hello,

I have a PySpark problem and maybe someone faced the same issue. I'm trying to read a xlsx file to a Pyspark dataframe using com.crealytics:spark-excel. The issue is that the xlsx file has values only in the A cells for the first 5 rows and the actual header is in the 10th row and has 16 columns (A cell to P cell).

When I am reading the file the df does not have all the columns.

Is there a specific way/ a certain jar file + pyspark version so that I can read all the data from the xlsx file and have the defacul header _c0 _c1 .... _c16 ?

Thank you !

Posted Jan 27, 2022, 5:30 PM UTC

44
dutch_gecko•

Have you checked spark-excel's documentation? The dataAddress option seems to be what you're looking for.

1
AnonymouseReddOP•

Using the dataAddress can solve the problem but this implies that I know at what cell the actual header starts. I would like to be able to read everything, to have everything in a dataframe and the filter something like df.where("_c0 like %My_header%").

Any ideas ?

1

Import it as csv. Importing as csv allows you to maintain the integrity of the original file and do your needed etl using python/ pyspark . Use Python to extract and transform your csv and pyspark to load into dataframe. Python allows you to read a csv as a list of tuples, which can be rearranged. Once you have the proper order load into a dataframe

1