Skip to content Skip to sidebar Skip to footer

Read Multiple Csv Files Into Separate Dataframes Using Pandas

I like to read two csv files from a particular folder into two separate dataframes. The two file names are: 23314621_MACI_NAV.CSV and 23314623_MACI_Holding.CSV The file second part

Solution 1:

If want create list of DataFrames:

dfs = []
for file in mscifile:
    df = pd.read_csv(file)
    dfs.append(df)

Or use list comprehension:

dfs = [pd.read_csv(file) for file in mscifile]

print (dfs[0])
print (dfs[1])

Another solution is create dictionary of DataFrames with keys by last substring after _ in filename:

from os.path import splitext, basename

dfs = {splitext(basename(fp))[0].split('_')[-1] : pd.read_csv(fp) for fp in mscifile}
print (dfs)

print (dfs['NAV'])
print (dfs['Holding'])

Post a Comment for "Read Multiple Csv Files Into Separate Dataframes Using Pandas"