Pandas Reading Each Xlsx File In Folder Code Example

Snippet 1

  import sys
import csv
import glob
import pandas as pd

# get data file names
path =r'C:DRODCL_rawdata_filesexcelfiles'
filenames = glob.glob(path + "/*.xlsx")

dfs = []

for df in dfs: 
    xl_file = pd.ExcelFile(filenames)
    dfs.concat(df, ignore_index=True) 

Snippet 2

  import os
import pandas as pd
import openpyxl as excel
import glob

#setting up path

path = 'data_inputs'
extension = 'xlsx'
files = [i for i in glob.glob('*.{}'.format(extension))]

#Grouping files - brings multiple files of same type together in a list 

wild_groups = ([s for s in files if "wild" in s])
domestic_groups = ([s for s in files if "domestic" in s])

#Sets up a dictionary associated with the file groupings to be called in another module 
file_names = {"WILD":wild_groups, "DOMESTIC":domestic_groups}

Snippet 3

  import pandas as pd
import glob

# your path to folder containing excel files
datapath = "\Users\path\to\your\file\"

# set all .xls files in your folder to list
allfiles = glob.glob(datapath + "*.xls")

# for loop to aquire all excel files in folder
for excelfiles in allfiles:
    raw_excel = pd.read_excel(excelfiles)

# place dataframe into list
list1 = [raw_excel]

Copyright © Code Fetcher 2020



Leave a comment

Your email address will not be published. Required fields are marked *