Read multiple CSV files into separate data frames
Quick draft, untested:
Use
list.files()
akadir()
to dynamically generate your list of files.This returns a vector, just run along the vector in a
for
loop.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.
Read in multiple csv into separate dataframes in Pandas
You can list all csv under a directory using os.listdir(dirname)
and combine it with os.path.basename
to parse the file name.
import os
# current directory csv files
csvs = [x for x in os.listdir('.') if x.endswith('.csv')]
# stats.csv -> stats
fns = [os.path.splitext(os.path.basename(x))[0] for x in csvs]
d = {}
for i in range(len(fns)):
d[fns[i]] = pd.read_csv(csvs[i])
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]
,...
How to load multiple csv files into seperate objects(dataframes) in R based on filename?
Solution for anyone curious...
files <- list.files(pattern = ".*csv")
for(file in 1:length(files)) {
file_name <- paste(c("file00",file), collapse = " ")
file_name <- gsub(" ", "", file_name, fixed = TRUE)
ex_file_name <- paste(c("exfile00",file), collapse = " ")
ex_file_name <- gsub(" ", "", ex_file_name, fixed = TRUE)
file_object <- read.csv(file = paste(file_name, ".csv", sep=""),fileEncoding="UTF-8-BOM")
exfile_object <- read.csv(file = paste(ex_file_name, ".csv", sep=""),fileEncoding="UTF-8-BOM")
}
Essentially build the filename within the loop, then passs it to the readcsv function on each iteration.
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.
Read multiple CSV files and replacing names
If you want to name each data frame you can read it into a list with lapply
and rename the elements of the list. Now you can access the data frames in the list. Also, you can use the three-letter abbreviations for the English month names, month.abb
.:
filenames = list.files(path = "C:/R/month_data/",
pattern = "2021-+.*csv")
filepath = paste0("C:/R/month_data/", filenames)
dfs <- lapply(filepath, read.csv)
names(dfs) <- month.abb
View(dfs$Jan)
Related Topics
Dplyr Filter: Get Rows With Minimum of Variable, But Only the First If Multiple Minima
Creating Arbitrary Panes in Ggplot2
Plot Multiple Lines in One Graph
How to Display Only Integer Values on an Axis Using Ggplot2
Aggregate Multiple Columns At Once
What Is Meaning of First Tilde in Purrr::Map
Dplyr: Inner_Join With a Partial String Match
How to Efficiently Calculate Distance Between Pair of Coordinates Using Data.Table :=
R Ifelse to Replace Values in a Column
Coalesce Two String Columns With Alternating Missing Values to One
Create Group Number For Contiguous Runs of Equal Values
How to Calculate the Co-Occurrence in the Table
Create Group Names For Consecutive Values
How to Unload a Package Without Restarting R