admin管理员组文章数量:1391964
I have an excel file that I'm reading into databricks using pyspark. The data has extra columns at the end that I do not want included. I use the following code to accomplish this:
data_object = spark.read.format("com.crealytics.spark.excel") \
.option("header", "true") \
.option("inferSchema", "true") \
.option("dataAddress", f"'1. ITEM'!A1") \
.load("path/to/file")
data_object = data_object.select(data_object.columns[0:21])
It then errors on the last line with the following:
AnalysisException: Column '`ITEM NUMBER
The entirety of the first column header is as follows:
'ITEM NUMBER\nMandatory Field\nFor Formula Calc. Only'
So, it appears that the line break is causing an issue, but if I attempt to perform a replace on all of the \n in the header row, I get the same error as above.
The ultimate goal is to rename the column headers to match the database using withColumnRenamed which does work. I also tried to then remove the extra columns (as opposed to right after reading the file like in the code above), but due to one of the extra columns having the same name as another column in the dataframe there is an ambiguity issue instead.
I have an excel file that I'm reading into databricks using pyspark. The data has extra columns at the end that I do not want included. I use the following code to accomplish this:
data_object = spark.read.format("com.crealytics.spark.excel") \
.option("header", "true") \
.option("inferSchema", "true") \
.option("dataAddress", f"'1. ITEM'!A1") \
.load("path/to/file")
data_object = data_object.select(data_object.columns[0:21])
It then errors on the last line with the following:
AnalysisException: Column '`ITEM NUMBER
The entirety of the first column header is as follows:
'ITEM NUMBER\nMandatory Field\nFor Formula Calc. Only'
So, it appears that the line break is causing an issue, but if I attempt to perform a replace on all of the \n in the header row, I get the same error as above.
The ultimate goal is to rename the column headers to match the database using withColumnRenamed which does work. I also tried to then remove the extra columns (as opposed to right after reading the file like in the code above), but due to one of the extra columns having the same name as another column in the dataframe there is an ambiguity issue instead.
Share Improve this question asked Mar 12 at 19:14 PracticingPythonPracticingPython 678 bronze badges 1- i can share a step by step approach on this, see if it helps your case – Debayan Commented Apr 8 at 6:05
1 Answer
Reset to default 0normalize_column_names
function: This removes line breaks (\n
) and extra spaces from the column headers. It ensures headers are clean and do not cause issues in later processing.select
: You explicitly select the first 21 columns by slicing the list of column names.withColumnRenamed
: This renames columns to match the desired names. If there are more columns to rename, you can extend thecolumn_mapping
dictionary.Handling duplicates: If you have duplicate column names after cleaning, consider appending a suffix (e.g.,
_duplicate
) to differentiate them.
# Step 1: Read the Excel file
data_object = spark.read.format("com.crealytics.spark.excel") \
.option("header", "true") \
.option("inferSchema", "true") \
.option("dataAddress", "'1. ITEM'!A1") \
.load("path/to/file")
# Step 2: Normalize column names
def normalize_column_names(columns):
return [col.replace("\n", "").strip() for col in columns]
data_object = data_object.toDF(*normalize_column_names(data_object.columns))
# Step 3: Select only the first 21 columns
data_object = data_object.select(data_object.columns[:21])
# Step 4: Rename columns
# Mapping of original column names to desired names
column_mapping = {
"ITEM NUMBERMandatory FieldFor Formula Calc. Only": "Item_Number",
# Add other mappings for remaining columns here
}
for old_name, new_name in column_mapping.items():
data_object = data_object.withColumnRenamed(old_name, new_name)
# Final DataFrame is clean and ready for database usage
data_object.show()
本文标签: headerIssue with renamingselecting columns in pysparkStack Overflow
版权声明:本文标题:header - Issue with renamingselecting columns in pyspark - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744732919a2622152.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论