r/learnpython • u/acfot • 12d ago
Everyone says 'just use pandas' for data stuff, but I'm still doing it with plain loops — am I actually missing out?
0
Upvotes
r/learnpython • u/acfot • 12d ago
5
u/castillar 12d ago
It’s absolutely fine to stick to plain Python until you actually need pandas! If you’re interested in data science, learning Pandas is a good idea because you’ll use it eventually, but I wouldn’t feel obliged to use it every time. I’m not in data science, but I get a lot more mileage out of the built-in CSV and JSON libraries even with fairly significant record sizes — I don’t usually have to reach for pandas until I have actual stats analysis or machine-learning bits to poke at.
2
u/ThrustBastard 12d ago
Right tool for the right job
2
u/BranchLatter4294 12d ago
It's fine if it works. But pandas is written in C/C++ so has much better performance for large datasets.
2
u/petros211 12d ago
Honestly, using libraries, frameworks, engines etc, doesn't teach you the actual programming required to do your goal, it teaches you specific things about the tool, that mostly doesn't convert to real knowledge. If you feel you can do something yourself there is nothing wrong with doing it this way.
2
u/shinitakunai 12d ago
If that's enough, it is enough.
If not I would consider polars over pandas
2
u/VegetableBid4262 12d ago
It’s not just about performance on large datasets. For someone who knows pandas, basic operations on even a small dataset are much faster to write.
1
u/toomanyteeth55 12d ago
I'm with you. Pandas has its place and it doesn't hurt to learn and be comfortable with it, but it seems like most tutorials will use pandas for even then generic thing that you can easily accomplish with it.
1
u/PaulRudin 12d ago
Python iteration is very slow. If your data is tiny it may not matter much, but numpy, pandas, polars etc. are mostly implemented in C, C++, Fortran, Rust and, when used properly, result in much, much faster executing code.
1
u/SteveJeffersonBots 12d ago
For a few hundred rows your loops are doing the job - pandas only starts paying off once you hit the patterns plain Python makes painful. Two concrete ones: joining two CSVs on a key (merging an orders sheet with a products sheet) and any "group-by-X then aggregate-Y" like "total sales per category" or "average per region." With loops that turns into nested dicts and index gymnastics; in pandas it's df.groupby('category')['amount'].sum() and pd.merge(left, right, on='product_id'). The speedup on a few hundred rows is modest, but the readability is the real win - your future self won't have to reverse-engineer a loop to change the grouping. So stick with loops until you actually need a join or a groupby, then reach for pandas for those two patterns specifically.
1
u/Discordbothost 12d ago
row count isn't really the tipping point, the kind of question is. counting and cleaning with loops and dicts stays fine forever. pandas starts paying for itself the first time you need "group by X, average Y, then match it against this other csv", or anything with dates. that's 15 lines of dict juggling vs df.groupby("x")["y"].mean().
good middle step meanwhile: collections.Counter and defaultdict. they turn most of your counting loops into one line and it's still plain python you can read back later
1
u/576p 12d ago
Pandas is awesome with thousands of lines. And it saves me a lot of time reading from the clipboard and reading and writing to excel files.
Looking back to 9 years ago when I tried to learn pandas I remember it was painful. The mental model of a virtual excel sheet helped me a lot to understand it.
But: You do not need pandas if you're fine with the performance and the convenience of writing the code. It's actually good if you can do it the slow way first before moving to a tool.
1
u/frustratedsignup 12d ago
I'm with you on this. We hired someone and management stole my Django project out from under me on his first day. He made a very complex time reporting system with most of the data being processed through pandas. Fast forward a couple of years, he leaves the company and I'm handed the task of managing the application he wrote. Great. Now I have to learn pandas to maintain the site.
I've looked at the documentation and it's fairly complex or at least it seems so on the surface. I'm going to work my way through a tutorial or two to get started.
1
u/RallyPointAlpha 10d ago
I was in your same shoes many years ago. Didn't bother learning Pandas until I needed to. Like you said: loops work just fine!
Then I started working with data sets with millions of rows. I got sick of waiting for things to process, while testing. Sure, I could test on a smaller data set but you never knew when you'd hit some weird anomaly 3 or 5 million rows in. So, eventually I would have to test on the whole data set.
What really sold me on Pandas was being able to do an operation on the entire data set in one call, without having to loop through the entire data set to apply it one at a time.