python - Function which changes strings of filenames based on a list -
i have multiple folders csv files in them, , creating cartesian lists , running statistics on file combinations.
so far executing this:
import pandas pd import os import scipy sp scipy import stats import glob import itertools # # path =r'f:\sheyenne\statistics\idl_stats\ndvi' # use path allfiles = glob.glob(path + "/*.csv") result = list(itertools.product(allfiles,allfiles)) # dataframe=[] files in result: x=(pd.read_csv(files[0], names = ['percent', 'value'])) z=x.percent y=(pd.read_csv(files[1], names = ['percent', 'value'])) d=y.percent stats2=sp.stats.ks_2samp(z,d) g=files, stats2 df=pd.dataframe(data=list(sum(g, ())), index=['file1', 'file2', 'd', 'p_value']).transpose() dataframe.append(df) df=pd.concat(dataframe) print df
but problem have multiple folders need navigate within pathway. r'f:\sheyenne\statistics\idl_stats\ndvi'
1 of many folders need execute code on. there way create function , change string of pathway this? if next folder location r'f:\sheyenne\statistics\idl_stats\ndii'
, have list names ndvi
, ndii
want automate run same code on next folder changing string (only portion after r'f:\sheyenne\statistics\idl_stats\
) within pathway based on items in list.
i hope makes sense.
create function:
def get_df(path): allfiles = glob.glob(path + "/*.csv") result = list(itertools.product(allfiles,allfiles)) # dataframe=[] files in result: x=(pd.read_csv(files[0], names = ['percent', 'value'])) z=x.percent y=(pd.read_csv(files[1], names = ['percent', 'value'])) d=y.percent stats2=sp.stats.ks_2samp(z,d) g=files, stats2 df=pd.dataframe(data=list(sum(g, ())), index=['file1', 'file2', 'd', 'p_value']).transpose() dataframe.append(df) return pd.concat(dataframe)
and use paths:
import os paths = [os.path.join(r'f:\sheyenne\statistics\idl_stats', name) name in ['ndvi', 'ndii']] dfs = [get_df(path) path in paths]
Comments
Post a Comment