I get "ValueError: columns overlap but no suffix specified" when I run my code for .xlsx files but not for .txt files. The data in these two different filetypes are identical. The following works fine:
import os
import pandas as pd
path = r'C:\Users\Me\1Test'
filelist = []
for root, dirs, files in os.walk(path):
for f in files:
if not f.endswith('.txt'):
continue
filelist.append(os.path.join(root, f))
for f in filelist:
df = pd.read_table(f)
col = df.iloc[ : , : -3]
df['Average'] = col.mean(axis = 1)
col1 = df.iloc[ :, 1 : -3]
df['Err'] = col1.sem(axis = 1)
out = (df.join(df.drop(df.columns[[-3,-1]], axis=1)
.sub(df[df.columns[-3]], axis=0)
.add_suffix(' - Background')))
out.to_excel(f.replace('txt', 'xlsx'), 'Analyzed Data')
The following gets the ValueError:
import os
import pandas as pd
path = r'C:\Users\Me\1Test'
filelist = []
for root, dirs, files in os.walk(path):
for f in files:
if not f.endswith('.xlsx'):
continue
filelist.append(os.path.join(root, f))
for f in filelist:
df = pd.read_excel(f)
col = df.iloc[ : , : -3]
df['Average'] = col.mean(axis = 1)
col1 = df.iloc[ :, 1 : -3]
df['Err'] = col1.sem(axis = 1)
out = (df.join(df.drop(df.columns[[-3,-1]], axis=1)
.sub(df[df.columns[-3]], axis=0)
.add_suffix(' - Background')))
out.to_excel('Analyzed Data')
Each file has a different amount of columns named 'ROI' + numbers and the 3rd to last column has a random name, which is the background. I want to run through the above functions for each file. Example df:
ROI005 | ROI008 | 53141 | AVG | ERR | |
---|---|---|---|---|---|
1 | 2 | 5 | 1 | 2.67 | 1.2 |
2 | 4 | 2 | 2 | 2.67 | .67 |
3 | 3 | 3 | 1 | 3 | 0 |
Desired output:
ROI005 | ROI008 | 53141 | AVG | ERR | ROI005 - Background | ROI008 - Background | Average - Background | |
---|---|---|---|---|---|---|---|---|
1 | 2 | 5 | 1 | 3.5 | 1.5 | 1 | 4 | 2.5 |
2 | 4 | 2 | 2 | 3 | 1 | 2 | 0 | 1 |
3 | 3 | 3 | 1 | 3 | 0 | 2 | 2 | 2 |