Thought leadership from the most innovative tech companies, all in one place.

How to Use Glob and Pandas to Read Multiple Filenames

Sometimes we need to work with multiple files. Glob is a handy Python package to read in multiple file names without having to manually write out the names of files. Of course, you could perform the…

Sometimes we need to work with multiple files. Glob is a handy Python package to read in multiple file names without having to manually write out the names of files. Bonus tip if you read until the end of this article!

import glob
import pandas as pd**# Change the file location to suit your needs**files = glob.glob('c:/users/{username}/documents/businesses/*.csv')**# Perform loop or if you want to be cool use a list comprehension** **# Create an empty list to store your data drames**
frame = []for file in files:
    frame.append(pd.read_csv(file)**# Concatenate Dataframes into a single Dataframe**df = pd.concat(frame)

image Hope this quick use case is helpful! Bonus tip for those of you who got to the end of my article:

# An extra tip if you to search your Dataframe for multiple termsindustries = ['Banking', 'Finance', 'Trucking']# Assume your dataframe has many industries and you want to search
# for 3 specific industries named above. You could search for
# a shorter or longer list. Convert to convert everything to lower
# or upper case to make sure that you get all matchesdf = df[df['Industry'].str.contains('|'.join(industries), na = False)]

And there you have it. Thank you for reading.




Continue Learning