Forum Discussion
Anonymous
2 years agoNot applicable
Load data from Semantic Model to Lakehouse where semantic model column names start with Capitals
Hi there - trying to lload a tabe from a semantic model to a lakehouse but the issue is that the table in the semantic model I want to load from has column names that start with Capital letters (for ...
frithjof_v
Community Champion
2 years agoChatGPT helped me with this. The main item in the code is this:
df_lower = df.toDF(*[c.lower() for c in df.columns])
ChatGPT created this example:
# Example DataFrame
data = [("John", 28), ("Alice", 23), ("Bob", 35)]
columns = ["Name", "Age"]
df = spark.createDataFrame(data, columns)
# Convert all column names to lowercase
df_lower = df.toDF(*[c.lower() for c in df.columns])
# Show the resulting DataFrame
df_lower.show()
Explanation:
- df.columns: Retrieves the list of column names.
- [c.lower() for c in df.columns]: Converts each column name to lowercase.
- df.toDF(*[c.lower() for c in df.columns]): Creates a new DataFrame with the lowercase column names.
This will rename all columns in the DataFrame to their lowercase equivalents.
Here is a StackOverflow thread mentioning the same method. Perhaps ChatGPT learned from it:
https://stackoverflow.com/questions/43005744/convert-columns-of-pyspark-data-frame-to-lowercase
So in your case, the code could be like this:
import sempy.fabric as fabric
#Dev
#workspace_ID = "XXXXXX"
#Stage
#workspace_ID = "XXXXXX"
#Prod
workspace_ID = "XXXXXX"
workspace_Safe_ID = workspace_ID.replace("-","_")
df_table = fabric.read_table("PSG_CTD_GDS_OMNI_Production", "_Environments",workspace=workspace_ID)
df_lower = df_table.toDF(*[c.lower() for c in df_table.columns])
df_lower.to_lakehouse_table("Environments", "overwrite")