简体   繁体   中英

Concatenate columns of dataframes in a loop Pandas

I have a dataset of csv files with two columns: wavelengths and absorbance values.

I'd like to do some statistical analysis within a loop, which contains a set of files, eg a mean absorbance file with standard deviation etc.

myfiles = sorted(glob.glob('blanks/Day01/Batch02/*.csv'))
mypath = 'blanks/Day01/Batch02/'
files         = [f for f in listdir(mypath) if isfile(join(mypath, f))]   # all files in a folder

for m in range(len(files)):
    df = pd.read_csv(mypath + files[m], skiprows=1, delimiter= ',')
    wl = df['Wavelength (nm)']
    A  = df['Abs']

My question is how do I "concatenate" 'Abs' columns from each file and then operate on them creating one single, mean/median one?

First create list of all DataFrames - filter columns by parameter usecols in read_csv and also is possible omit delimiter=',' because default parameter:

dfs = []
for m in range(len(files)):
    df = pd.read_csv(mypath + files[m], 
                     skiprows=1, 
                     usecols = ['Wavelength (nm)', 'Abs']) # usecols = ['Abs'] for filter Abs
    dfs.append(df)

Alternative:

dfs = [pd.read_csv(mypath + files[m], skiprows=1, usecols = ['Wavelength (nm)', 'Abs']) for m in range(len(files))]

And last concat together:

df = pd.concat(dfs, ignore_index=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM