Skip to content

Instantly share code, notes, and snippets.

@aphor
Created October 3, 2018 00:10
Show Gist options
  • Save aphor/aea495d0b576c8cdd0db0b3d70561702 to your computer and use it in GitHub Desktop.
Save aphor/aea495d0b576c8cdd0db0b3d70561702 to your computer and use it in GitHub Desktop.
ABC DEF files
import os, pandas
cwd = os.getcwd() # this is where to find the csv directories and files
dfs = {} # this is where we will put the pandas DataFrame for each color
for root, dirs, files in os.walk(cwd):
for fn in files:
if fn.endswith(".csv"): # skip non csv files silently
color = fn.split('.',3)[1] # extract color name like 'ABC' or 'DEF'
fqpn = os.path.sep.join((root,fn)) # construct a filename to read
if color in dfs: # append existing color DataFrame if we find a new file
dfs[color].append(pandas.read_csv(fqpn))
else: # otherwise create a new color DataFrame from this csv
dfs[color] = pandas.read_csv(fqpn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment