Tuesday Tip #40: Build your DataFrame from multiple files 🏗️


Hi Reader,

In case you missed it, I launched a free, 7-hour pandas course!

800+ students have enrolled, and a few have already earned their certificate of completion 👩‍🎓


đź”— Link of the week

​Data Internships​

Looking for an internship in Data Science or Analytics? This site curates the latest internship postings and emails them to you each week!


👉 Tip #40: Build a DataFrame from multiple files

Let’s say that your dataset is spread across multiple files, but you want to read the dataset into a single pandas DataFrame.

For example, I have a tiny dataset of stock market data in which each CSV file only includes a single day. Here’s the first day:

Here’s the second day:

And here’s the third day:

You could read each CSV file into its own DataFrame, combine them together, and then delete the original DataFrames, but that would be memory inefficient and require a lot of code.

A better solution is to use Python’s built-in glob module:

You can pass a pattern to the glob() function, including wildcard characters, and it will return a list of all files that match that pattern.

In this case, glob() is looking in the “data” subdirectory for all CSV files that start with the word “stocks” followed by one or more characters:

glob returns filenames in an arbitrary order, which is why we sorted the list using Python’s built-in sorted() function.

We can then use a generator expression to read each of the files using read_csv() and pass the results to the concat() function, which will concatenate the rows into a single DataFrame:

Unfortunately, there are now duplicate values in the index. To avoid that, we can tell the concat() function to ignore the index and instead use the default integer index:

Pretty cool, right?

Need to build a DataFrame column-wise instead? Use the same code as above, except pass axis='columns' to concat()!


đź‘‹ Until next time

Did you like this week’s tip? Please forward it to a friend or share this link in your favorite Slack team. It really helps me out! 🙌

See you next Tuesday!

- Kevin

P.S. Would you wear pajamas during a Zoom call?​

Did someone AWESOME forward you this email? Sign up here to receive Data Science tips every week!

Learn Artificial Intelligence from Data School 🤖

Join 25,000+ intelligent readers and receive AI tips every Tuesday!

Read more from Learn Artificial Intelligence from Data School 🤖

Hi Reader, Here are your top AI stories for the week: ChatGPT can weaken your brain Claude shares nerve gas recipe Amsterdam ends AI experiment due to bias Read more below! 👇 Sponsored by: Brain.fm Transform Your Focus With Brain.fm I know you're always on the hunt for tools that genuinely improve your life—which is why I'm excited to introduce you to Brain.fm's groundbreaking focus music. Brain.fm's patented audio technology was recently validated in a top neuroscience journal, showing how...

Hi Reader, Last week, I invited you to help me test Google's Data Science Agent in Colab, which promises to automate your data analysis. Does it live up to that promise? Let's find out! 👇 Sponsored by: Morning Brew Business news you’ll actually enjoy Join 4M+ professionals who start their day with Morning Brew—a free daily newsletter that makes business, tech, and finance news genuinely enjoyable to read and hard to forget. Each morning, it breaks down complex stories in plain English—cutting...

Hi Reader, Today I'm trying something brand new! I wrote short summaries of the 5 most important AI stories this week, and also turned it into a video: Watch the video I'd love to know what you think! đź’¬ AI-generated TV ad airs during NBA finals Prediction market Kalshi just aired this AI-generated ad on network TV during the NBA finals. It was created in just two days by one person using Google's new Veo 3 video model, plus scripting help from Google's Gemini chatbot. Expect to see many more...