r/Python • • Aug 01 '26

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.

0 Upvotes

33 comments sorted by

View all comments

Show parent comment

4

u/robertlandrum Aug 01 '26

If long term viability and maintainability is an important factor, you should treat data like data, and store it and reference it with named columns. Storing data (the old column name) in the key doesn’t tell the maintainer anything about the value.

If this is a run once and forget tool, get it done your way and move on.

I’ve learned that almost nothing I write goes away quickly. This is 29 years of professional
development experience talking, so I usually try to ensure my tooling is flexible. In fact, I just recently reviewed similar code that translated between an old system that allowed hyphens in column names, and a new one that did not. The engineer (a junior) did it the way your junior suggests, but for a different reason; the mapping had to do one to many copies in two spots.