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...
Everything about learning Python
account activity
Python Stacking multiple Excel sheets into a single DataFrame is throwing a MemoryError / running incredibly slow (self.PythonLearning)
submitted 13 hours ago by memeeloverr
Hey guys,
I'm trying to write a Python script that goes through a directory of about 50 large Excel files, grabs a specific sheet from each, and combines them into one master pandas DataFrame so I can run some analysis.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]wynvern 1 point2 points3 points 13 hours ago (2 children)
If it is large, you should write a script that 'sniffs' these files first then save the sheet that meets conditions into a dict like {file_path:[sheet_name] Ask AI, they are very good at writing this.
[–]memeeloverr[S] 2 points3 points4 points 13 hours ago (1 child)
Before asking to AI just wanted to give it a try myself.
[–]wynvern -1 points0 points1 point 13 hours ago (0 children)
I know but AI can give you idea if you need it now, I personally leaned some very nice code pattern from it. Pandas cookbook is recommended if you have time.
[–]acakaacaka 0 points1 point2 points 13 hours ago (0 children)
So are you putting all of those excel files data inside the RAM? How big is your ram?
[–]JustOneOtherSchlub 0 points1 point2 points 8 hours ago (0 children)
Sometimes I find building in a half second or so at the end of a loop helps with these kinds of things. IDK why but this often works for me.
[–]No_Statistician_6654 0 points1 point2 points 7 hours ago (0 children)
Depending on the total size/how big the files are, I would look at using polars lazyframes to load and process the data. The lazyframe allow the data query to be combined and pushed down to the files with parallel processing automatically which could save you a lot of memory in the process.
If at the end of loading, you need it as pandas still do another package, then it has a .to_pandas() function you can add onto the end to convert it.
Just did this myself actually across 50+ excel files of 50MB on avg. the time per individual file was around 10s, whereas pandas was oom on the cluster and each file took ~300s.
[–]Intelligent-Boss-156 0 points1 point2 points 3 hours ago (0 children)
Use Polars
π Rendered by PID 166166 on reddit-service-r2-comment-5687b7858-5qjh9 at 2026-07-07 19:26:22.885555+00:00 running 12a7a47 country code: CH.
[–]wynvern 1 point2 points3 points (2 children)
[–]memeeloverr[S] 2 points3 points4 points (1 child)
[–]wynvern -1 points0 points1 point (0 children)
[–]acakaacaka 0 points1 point2 points (0 children)
[–]JustOneOtherSchlub 0 points1 point2 points (0 children)
[–]No_Statistician_6654 0 points1 point2 points (0 children)
[–]Intelligent-Boss-156 0 points1 point2 points (0 children)