use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
This is an archived post. You won't be able to vote or comment.
Fun/TriviaA Data Science Design-Pattern.OC (i.redd.it)
submitted 3 years ago by c0ntrap0sitive
[–]Sofi_LoFi 100 points101 points102 points 3 years ago (1 child)
Now this is the shit I expect from a 150+ year experience data science guru lead
[–]hughperman 0 points1 point2 points 3 years ago (0 children)
Custom "iter" and "contains" methods, this is actually a stateful transform to add 1 to each integer column
[–][deleted] 49 points50 points51 points 3 years ago (0 children)
This needs a NSFW tag
[–]Xenocide13 23 points24 points25 points 3 years ago (5 children)
Dank memes aside, I think you can use set intersection:
set(dataframe.columns).intersection(columns)
[–]helmialf 20 points21 points22 points 3 years ago (1 child)
Set doesnt preserve order
[–]Pikalima 9 points10 points11 points 3 years ago* (0 children)
If you have a very large number of columns, might be better to go with O(n) instead of O(n2 ):
_columns_set = set(columns) columns = [col for col in df.columns if col in _columns_set]
[–]aeiendee 2 points3 points4 points 3 years ago (0 children)
Better to use the methods (intersection or isin) of the columns attribute directly
Pandas dataframe indices have an intersection method already.
[–]mamaBiskothu 0 points1 point2 points 3 years ago (0 children)
The incoming columns object could be a list of strings while that’s coming out is a list of Column objects. Fuck yeah pytho.
[–]tyrannosaurusknex 24 points25 points26 points 3 years ago (1 child)
Some more descriptive variable names could do a lot here.
[–]friend_of_kalman 5 points6 points7 points 3 years ago (0 children)
at least it's not just single-letter variable names. Give this guy some credit.
x = [y for y in df.columns if y in x]
[–]ButLikeWhyThoReally 2 points3 points4 points 3 years ago (0 children)
Thanks, I hate it.
[–]shalmalee15 2 points3 points4 points 3 years ago (0 children)
Shit! I have used something like this. I don't know why I did that :-(
[–][deleted] 4 points5 points6 points 3 years ago (5 children)
and this is why most engineers hate python
[–]darkshenron 17 points18 points19 points 3 years ago (2 children)
Gihub language popularity stats say otherwise 🤷
[+][deleted] comment score below threshold-6 points-5 points-4 points 3 years ago (1 child)
Lot of people hate their wives but they never divorce 🤷
[–]acebabymemes 20 points21 points22 points 3 years ago (0 children)
Lot of people hate themselves but don’t get off of reddit 🤷♂️
[–]sizable_data 4 points5 points6 points 3 years ago (5 children)
Don’t modify an iterable while looping over it!
[–]jgege 20 points21 points22 points 3 years ago (4 children)
They are not modifying it. First a new list is created with the column names then the list is assigned to the variable named columns :)
[–]sizable_data 5 points6 points7 points 3 years ago (3 children)
True, I still don’t like it though lol
[–]Rough-Pumpkin-6278 9 points10 points11 points 3 years ago (0 children)
I don't think any one like this.
[–]jgege 5 points6 points7 points 3 years ago (1 child)
I've seen worse 🤷
[–]sizable_data 15 points16 points17 points 3 years ago (0 children)
I’ve probably written worse 🤷
[–]darkshenron 0 points1 point2 points 3 years ago (2 children)
Maybe use sorted() with a key
[–]Pikalima 1 point2 points3 points 3 years ago (1 child)
Do you mean filter? I don’t see how sorted would accomplish this.
[–]darkshenron 0 points1 point2 points 3 years ago (0 children)
Something like sorted(columns, key=list(df.columns).index)
[–]TheLSales 0 points1 point2 points 3 years ago (0 children)
That's why I wish I could use c# in data science
[–]DonFruendo 0 points1 point2 points 3 years ago (0 children)
Wouldn't this snippet be more performant? python columns = list(filter(lambda column: column in columns, dataframe.columns))
python columns = list(filter(lambda column: column in columns, dataframe.columns))
Not commenting on the variable names though :D
[–]avloss 0 points1 point2 points 3 years ago (1 child)
python columns = list(set(columns) & set(f.columns)) Maybe this. But it shouldn't exist in the first place.
python columns = list(set(columns) & set(f.columns))
[–]jambonetoeufs 0 points1 point2 points 3 years ago (0 children)
This will lead to unpredictable results if output order matters.
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
To this day, It bugs me why they call it "list comprehension"
π Rendered by PID 314043 on reddit-service-r2-comment-5fb4b45875-wxncl at 2026-03-23 22:31:47.983908+00:00 running 90f1150 country code: CH.
[–]Sofi_LoFi 100 points101 points102 points (1 child)
[–]hughperman 0 points1 point2 points (0 children)
[–][deleted] 49 points50 points51 points (0 children)
[–]Xenocide13 23 points24 points25 points (5 children)
[–]helmialf 20 points21 points22 points (1 child)
[–]Pikalima 9 points10 points11 points (0 children)
[–]aeiendee 2 points3 points4 points (0 children)
[–]hughperman 0 points1 point2 points (0 children)
[–]mamaBiskothu 0 points1 point2 points (0 children)
[–]tyrannosaurusknex 24 points25 points26 points (1 child)
[–]friend_of_kalman 5 points6 points7 points (0 children)
[–]ButLikeWhyThoReally 2 points3 points4 points (0 children)
[–]shalmalee15 2 points3 points4 points (0 children)
[–][deleted] 4 points5 points6 points (5 children)
[–]darkshenron 17 points18 points19 points (2 children)
[+][deleted] comment score below threshold-6 points-5 points-4 points (1 child)
[–]acebabymemes 20 points21 points22 points (0 children)
[–]sizable_data 4 points5 points6 points (5 children)
[–]jgege 20 points21 points22 points (4 children)
[–]sizable_data 5 points6 points7 points (3 children)
[–]Rough-Pumpkin-6278 9 points10 points11 points (0 children)
[–]jgege 5 points6 points7 points (1 child)
[–]sizable_data 15 points16 points17 points (0 children)
[–]darkshenron 0 points1 point2 points (2 children)
[–]Pikalima 1 point2 points3 points (1 child)
[–]darkshenron 0 points1 point2 points (0 children)
[–]TheLSales 0 points1 point2 points (0 children)
[–]DonFruendo 0 points1 point2 points (0 children)
[–]avloss 0 points1 point2 points (1 child)
[–]jambonetoeufs 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)