Reading Multiple Files into Multiple Data Frames

Read multiple CSV files into separate data frames

Quick draft, untested:

  1. Use list.files() aka dir() to dynamically generate your list of files.

  2. This returns a vector, just run along the vector in a for loop.

  3. Read the i-th file, then use assign() to place the content into a new variable file_i

That should do the trick for you.

Reading multiple files into separate data frames

You can probably do something like this:

import glob
d = {}
base_name = "df{}"
flag = 0

for filename in glob.glob('*.xlsx'):
d[base_name.format(flag)] = pd.read_excel(filename, sheet_name = 'Bilan')
flag += 1

Here you create a base_name for your name and a flag to track the position of your file and then use those variables to construct a full filename.

reading multiple csv and saving into separate dataframes

seems like you're overwriting the same variable again and again

path = os.getcwd()
csv_files = glob.glob(os.path.join(r'blabla/Data', "*.csv" ))

list_of_dfs = []

for f in csv_files:
df = pd.read_csv(f)
print('Location:', f)
print('File Name:', f.split("\\")[-1])
print('Content:')
df.pop('Unnamed: 0')
display(df)
list_of_dfs.append(df)

access the individual dataframes with list_of_dfs[0], list_of_dfs[1],...

Reading multiple files into separate data frames in PYTHON

I believe you need create dictionary of DataFrame with keys by filenames:

d = {}
for filename in glob.glob('*.csv'):
if "test" in filename:
d[filename[:-4]] = pd.read_csv(filename)

What is same as:

d = {f[:-4]: pd.read_csv(f) for f in glob.glob('*.csv') if "test" in f}

If want only name of file is possible use:

d = {os.path.basename(f).split('.')[0]:pd.read_csv(f) for f in glob.glob('*.csv') if "test" in f}

Reading multiple txt files into data frames and merging them into one

The following should work well. However, without sample data or a more clear description of what you want it's hard to know for certain if this if what you are looking to accomplish.

#set working directory
setwd("C:/Users/path/to/my/files")

#read in all .txt files but skip the first 8 rows
Data.in <- lapply(list.files(pattern = "\\.txt$"),read.csv,header=T,skip=8)

#combines all of the tables by column into one
Data.in <- do.call(rbind,Data.in)

Retrieving data from multiple files into multiple dataframes

You are really close, need join all data by concat from generator:

contentdataframes = (pd.read_excel(f) for f in listoffiles)
df = pd.concat(contentdataframes, ignore_index=True)

If need list of DataFrames:

contentdataframes = [pd.read_excel(f) for f in listoffiles]


Related Topics



Leave a reply



Submit