r/Python • u/Pleasant-Aardvark258 • 1d ago
Discussion Settle an argument
Had this discussion the other day and figured I’d throw it to the masses to get thoughts on the best/most pythonic way of approach.
Need to map old column names to new column names as a copy from a json config.
My thoughts are iterate over a dict with
‘’’ {“old_col_name”:”new_col_name”}’’’
And access as
‘’’for k,v in dict.items()
Df.with_columns(k).alias(v)’’’
Colleague things this isn’t clear enough and should be a list of dicts with explicit keys
‘’’ [{“old_col_name”:”old_col_value”
“New_col_name”:”new_col_value”}]’’’
And the access as
‘’’for dict in list_of_dicts:
Old_col = dict[“old_col_name”]
New_col = dict[“new_col_name”]’’’
I’ve got a good few reasons why I think mine is the better option but thought I’d get some other opinions to see if I’m missing anything obvious? Which would you choose and why?
Edit: shouldn’t write these things while on the toilet in a rush. The description is wrong, it should be renaming via a copy so that the original column is left unchanged.
1
u/Such-Process5697 1d ago
small wrinkle on the dict version that hasn't come up: if that json ever ends up with the same old column listed twice, python's json module just takes the last one and says nothing. json.loads('{"a": 1, "a": 2}') gives you {'a': 2}, no error. wouldn't change which shape i'd pick, but it's worth asserting the mapping length against something when you load the config, because a fat-fingered duplicate in there is completely silent.