I have one big origin table which I want to split in several smaller tables based on the category.
| id | Category | color |
|---|---|---|
| 1 | car | yellow |
| 2 | plane | blue |
| 3 | plane | green |
| 4 | car | blue |
| 5 | bus | blue |
| 5 | plane | yellow |
Table name: car
| id | Category | color |
| ---| -------- | ----- |
| 1 | car | yellow|
| 4 | car | blue |
Table name: plane
| id | Category | color |
|---|---|---|
| 2 | plane | blue |
| 3 | plane | green |
| 5 | plane | yellow |
Table name: bus
| id | Category | color |
|---|---|---|
| 5 | bus | blue |
This is one potential solution:
df_tmp = df_transportation.groupBy("category").count()
categories = list(df_tmp["category"])
for category in categories:
df_cat = df_transportation.filter(col("category") == "category))
df_cat.write.mode("overwrite").format("delta").saveAsTable(f'{category}'
Is there a way to get to the same solution without a for loop?