Creating And Assigning Different Variables Using A For Loop
So what I'm trying to do is the following: I have 300+ CSVs in a certain folder. What I want to do is open each CSV and take only the first row of each. What I wanted to do was th
Solution 1:
If i understand your requirement correctly, we can do this quite simply, lets use Pathlib instead of os
which was added in python 3.4+
from pathlib import Path
csvs = Path.cwd().glob('*.csv') # creates a generator expression.#change Path(your_path) with Path.cwd() if script is in dif location
dfs = {} # lets hold the csv's in this dictionaryfor file in csvs:
dfs[file.stem] = pd.read_csv(file,nrows=3) # change nrows [number of rows] to your spec.#or with a dict comprhension
dfs = {file.stem : pd.read_csv(file) for file in Path('location\of\your\files').glob('*.csv')}
this will return a dictionary of dataframes with the key being the csv file name .stem
adds this without the extension name.
much like
{
'csv_1' : dataframe,
'csv_2' : dataframe
}
if you want to concat these then do
df = pd.concat(dfs)
the index will be the csv file name.
Post a Comment for "Creating And Assigning Different Variables Using A For Loop"