Skip to content

Instantly share code, notes, and snippets.

@hxhc
Last active July 13, 2017 08:32
Show Gist options
  • Save hxhc/d5bf89b90b86d40165033d8d4fa65ebe to your computer and use it in GitHub Desktop.
Save hxhc/d5bf89b90b86d40165033d8d4fa65ebe to your computer and use it in GitHub Desktop.
merge some data files into one
import pandas as pd
import os
import glob
import numpy as np
# This script is used to calculate the mean of spectra of every 3 jdx files, thus combining 3 data files into one.
# get all filenames
path = os.getcwd()
all_files = glob.glob(os.path.join(path, "*.txt"))
data = pd.DataFrame()
# read and combine all files in one dataframe
# an alternative way
# data = pd.concat(pd.read_table(f) for f in all_files)
for file_ in all_files:
data = pd.concat([data, pd.read_table(file_, names=["X", file_])], axis=1)
del data["X"] # delete all x_values(wavenumber)
#merge every 3 columns into 1, say, using mean_value
temp_data = np.array(data[1::], dtype=float)
j=0
data2 = np.zeros([temp_data.shape[0], temp_data.shape[1]/3])
for i in np.arange(0, temp_data.shape[1]-1, 3):
mean_value = (temp_data[:,i]+temp_data[:,i+1]+temp_data[:,i+2])/3
data2[:,j] = mean_value
j=j+1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment