- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2025 12:13 PM
I'm migrating code from Python Linux to Databricks PySpark. I have many mappings like this:
{
"main": {
"honda": 1.0,
"toyota": 2.9,
"BMW": 5.77,
"Fiat": 4.5,
},
}
I exported using json.dump, saved to s3 and was able to import with spark.read.json, but that puts it into a dataframe with nested objects, like this. I can access the objects with select but don't know how to apply it as a mapping to another dataframe.
Another approach is to save as JSON Lines and read in as a dictionary? From what I understand PySpark prefers to have JSON lines rather than usual JSON, like this:
{"main":{"honda":1,"toyota":2,"BMW":5,"Fiat":4}}
Ok so I manually created the dict and mapping. Given a dictionary car_map with the values, and a dataframe df: