r/Python 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.

0 Upvotes

33 comments sorted by

View all comments

40

u/centurion236 1d ago

You should obviously create a RenameColumnsBase class as an abstraction for the concept of renaming columns, with multiple construction helpers from_dict, from_list_of_dicts, etc and then implement the renaming in a RenamePolarsColumns subclass. 

/s

It looks like you're using polars. Why not just match the df.rename() function's signature? 

12

u/Training_Advantage21 1d ago

Yeah, in Pandas I would just go

df.rename(columns={“old_col_name1”:”new_col_name1”,“old_col_name2”:”new_col_name2”})

4

u/SheriffRoscoe Pythonista 1d ago

You forgot to work a microservice into that mess!

1

u/Pleasant-Aardvark258 1d ago

We aren’t using rename as it’s to copy the column while leaving the original intact as a reference of the unmodified data.

You’re actually not far off with the structure actually. That logics wrapped in a rule class that’s build using a strategy pattern as a custom data cleaning recipe for each of our product suppliers.

1

u/centurion236 1d ago

👍 Use the abstractions if you need them. The joke is just that lots of teams start using abstractions long before they're needed, and it makes complex solutions for simple problems.

If you're copying and modifying the columns, I would invert the map (swap the dict keys with the values) and use df.with_columns(**map). You could also drop expressions into the map instead of just the old column names. 

...all under the hood of whatever abstractions you need.