Put Csv Files In Separate Pandas Dataframes Depending On Filename
I have a list that contains file names. I want to parse directory and read all the files starting with every element from list and store it in dataframe Eg: list1=[abc,bcd,def] Di
Solution 1:
I'm assuming you have a directory where there could be several other files besides the ones you want to read.
import os
import pandas as pd
dfs = { }
for f in os.listdir(dirname):
for k in list1:
if f.startswith(k):
try:
dfs[k].concat(pd.read_csv(dirname+f, sep=',', header=None))
except KeyError:
dfs[k] = pd.read_csv(dirname+f, sep=',', header=None))
Solution 2:
This will create a dictionary of DataFrames
where each DataFrame
consists of all files matching the first three letters of our "expressions" (i.e. abc
, def
et.c.). The keys in the dictionary are the same three letters:
# Some dummy data
filenames = ['abcdefghijkl.txt', 'abcdef.txt', 'defghijk.txt']
# List of combination of certain letters
exps = ['abc', 'def', 'ghi', 'jkl']
dataframes = {}
for filename in filenames:
_df = pd.read_csv(filename)
key = exps[exps.index(filename[:3])]
try:
dataframes[key] = pd.concat([dataframes[key], _df], ignore_index=True)
except KeyError:
dataframes[key] = _df
print(dataframes['abc'])
a b c
0789110111221233456print(dataframes['def'])
a b c
07891101112
The contents of the files above are:
abcdefghijkl.txt
a,b,c
7,8,910,11,12
abcdef.txt
a,b,c
1,2,34,5,6
defghijkl.txt
a,b,c
7,8,910,11,12
Post a Comment for "Put Csv Files In Separate Pandas Dataframes Depending On Filename"