I'm pretty sure there's multiple different variables here with the name "column", and you can only know which one is meant based on the specific syntax rules of this kind of a statement. So probably rename one to "column_to_find" and "column_from_table" or something along those lines, to make it more clear which is which.
Clear variable names are very important. Don't just say what something is, like "column" because it is a column. No, what is the purpose of the column, add that to the name. Don't just call a list of columns "columns". If you're using it as a filter to find columns that match the ones in that list, call it "valid_columns", etc etc
I just use x as a comprehension var to avoid this situation. Yes it's less descriptive, but it does not matter if it's scope is one line.
EDIT: oh, but if you mean performance-wise, it's fine if columns is small enough, but if it's super large, you should be using sets. And in this code, maybe you are if both are sets. But proper way would be intersecting them.
The way I see it, a set is a group of objects, just like a list. The only difference is that it doesn't allow repeated items. If I try to essentially cast a list to a set, I wouldn't immediately expect the underlying structure to change so it fits the requirements, I would expect an error to be raised if the format of my input is invalid for the cast.
This isn't a huge problem or anything, but it's not what my first thought would be.
It's actually an OK way to get the intersection of the initial list of columns columns and the columns that are actually in the dataframe dataframe.columns.
A dict remembers its insertion order from Python 3.7 onward. Python has no ordered set, but you could use collections.OrderedDict and ignore the values of the key-value pairs in place of an ordered set.
Is this actually bad code? I feel like I would know exactly what this was doing if I was reading someone else's work. There are other ways to do this in this thread, but the list comprehension is the most obvious on what it's doing in my opinion.
118
u/mrstickman Mar 23 '23
semantic satiation
61
u/dipodomys_man Mar 23 '23
I feel like this is code I have written before….Whats the better way to find the matching column names? Or am I missing something?
A quick search link suggests sets, is that the answer?
26
u/trutheality Mar 23 '23
It's actually an OK way to get the intersection of the initial list of columns
columnsand the columns that are actually in the dataframedataframe.columns.With sets you could do
It might be faster, but it doesn't necessarily preserve column order.
If
dataframeis apandas.DataFrame, you can also go full pandas with:These are perhaps a bit more explicit about what the code is doing, so you may or may not view them as more readable.
7
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 23 '23
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
6
u/Aulentair Mar 23 '23
column
3
u/ChummusJunky Mar 24 '23
Columns?
3
u/sofanisba Mar 24 '23
This pattern is called a Columbo
3
u/thekwoka Mar 24 '23
This syntax is one of the things I really hate about Python.
It's awful to read even with good names.
9
u/00PT Mar 23 '23
I'd rename "column" to something like "col" or just "c". Otherwise, this isn't particularly bad code.
2
u/abubalesh Mar 24 '23
I recently learned set(columns).issubset(data frame.columns.tolist()) and it’s much cleaner
2
u/WpGgs Mar 24 '23
Every column from dataframe.columns if they are in columns. It's a little verbose but not bad.
2
u/ThatNextAggravation Mar 24 '23
An eye for an eye, a column for a column.
2
u/bolapolino Mar 24 '23
What? This seems legit. Long live pandas :D
2
u/dubyanue Mar 24 '23
Code looks fine for the most part, variable names could be changed to be clearer
2
u/IlliterateJedi Mar 24 '23
Is this actually bad code? I feel like I would know exactly what this was doing if I was reading someone else's work. There are other ways to do this in this thread, but the list comprehension is the most obvious on what it's doing in my opinion.
2
u/Volt__3000 Mar 24 '23
What font is this? Does anybody know?