python - How to create separate Pandas DataFrames for each CSV file and give them meaningful names? -
i've searched thoroughly , can't quite find guidance looking on issue hope question not redundant. have several .csv files represent raster images. i'd perform statistical analysis on them trying create pandas dataframe each file can slice 'em dice 'em , plot 'em...but having trouble looping through list of files create df meaningful name each file.
here have far:
import glob import os pandas import * #list of .csv files #i'd turn each file dataframe datalist = glob.glob(r'c:\users\charlie\desktop\qvik\textrasters\*.csv') #name i'd use each data frame namelist = [] raster in datalist: path_list = raster.split(os.sep) name = path_list[6][:-4] namelist.append(name) #zip these lists dict datadct = {} k, v in zip(namelist,datalist): datadct[k] = datadct.get(k,"") + v datadct
so have dict key name want each dataframe , value path read_csv(path):
{'aspect': 'c:\\users\\charlie\\desktop\\qvik\\textrasters\\aspect.csv', 'curvature': 'c:\\users\\charlie\\desktop\\qvik\\textrasters\\curvature.csv', 'normalz': 'c:\\users\\charlie\\desktop\\qvik\\textrasters\\normalz.csv', 'slope': 'c:\\users\\charlie\\desktop\\qvik\\textrasters\\slope.csv', 'snowdepth': 'c:\\users\\charlie\\desktop\\qvik\\textrasters\\snowdepth.csv', 'vegetation': 'c:\\users\\charlie\\desktop\\qvik\\textrasters\\vegetation.csv', 'z': 'c:\\users\\charlie\\desktop\\qvik\\textrasters\\z.csv'}
my instinct try variations of this:
for k, v in datadct.iteritems(): k = read_csv(v)
but leaves me single dataframe, 'k' , filled data last file read in loop.
i'm missing fundamental here starting spin wheels on i'd thought i'd ask y'all...any ideas appreciated!
cheers.
are trying of data frames separately in dictionary, 1 data frame per key? if so, leave dict showed instead have data in each key.
datadct = {} k, v in zip(namelist,datalist): datadct[k] = read_csv(v)
so now, example:
datadct['snowdepth'][['cola','colb']].plot()
Comments
Post a Comment