熊猫"ValueError: columns overlap but no suffix specified".xlsx文件,但不.txt文件



我得到了"ValueError:列重叠但没有指定后缀";当我为.xlsx文件而不是为.txt文件运行代码时。这两种不同文件类型中的数据是相同的。以下操作很好:

import os
import pandas as pd
path = r'C:UsersMe1Test'
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')

以下获取ValueError:

import os
import pandas as pd
path = r'C:UsersMe1Test'
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')

每个文件都有不同数量的列,名称为"ROI"+数字,从第三列到最后一列有一个随机名称,即背景。我想为每个文件运行以上函数。示例df:

ROI005123

我不明白你想做什么,所以我只能给出一些一般的提示。

如果join数据帧具有一个或多个相等的列名,则给定的错误会引发,因此panda无法区分它们。如果我把你的代码读对了,你就用df本身加入了,所以当然会有相同的列。为了更好地了解发生了什么,您可以像Error试图告诉您的那样添加lsuffixrsuffix(这有点神秘(。这将";"修复";错误,并在列上创建后缀。

df.join(..., lsuffix='_left', rsuffix='_right')

在后台,join使用merge并在索引上进行合并。您也可以直接使用merge(具有相同的输出(。这有时更容易,因为它可以更好地处理列(不会得到特定的错误((参见SO中的其他问题(

这里有一个关于selfjoin的简单例子:SO问题

这里他们也使用后缀。

我不知道为什么错误只从excel文件中出现,但我认为数据不一样(阅读后(。

编辑

我已经用你的数据测试了你的代码,没有得到任何错误。我用这个创建了数据:

df = pd.DataFrame(columns=["ROI005", "ROI008", "53141", "AVG", "ERR"],
data=[
[2, 5, 1, 2.67, 1.2],
[4, 2, 2, 2.67, 0.67],
[3, 3, 1, 3, 0]]
)

这导致:

ROI005  ROI008  53141   AVG   ERR
0       2       5      1  2.67  1.20
1       4       2      2  2.67  0.67
2       3       3      1  3.00  0.00

在你的脚本之后(第二个(:

ROI005  ROI008  53141   AVG   ERR  Average  Err  ROI005 - Background  ROI008 - Background  53141 - Background  AVG - Background  Average - Background
0       2       5      1  2.67  1.20      3.5  2.0                 0.80                 3.80               -0.20              1.47                  2.30
1       4       2      2  2.67  0.67      3.0  0.0                 3.33                 1.33                1.33              2.00                  2.33
2       3       3      1  3.00  0.00      3.0  1.0                 3.00                 3.00                1.00              3.00                  3.00

AVGAverage相同吗?ErrERR相同吗?我认为你必须提供更多的数据,更加具体。使用相同的两列,我得到:

ROI005  ROI008  53141  AVG  ERR  ROI005 - Background  ROI008 - Background  AVG - Background
0       2       5      1  3.5  NaN                    1                    4               2.5
1       4       2      2  3.0  NaN                    2                    0               1.0
2       3       3      1  3.0  NaN                    2                    2               2.0

这几乎是你想要的(只有ERR是错误的(

如果您保存了索引,但没有读取它(因此它会生成一个新索引,并将旧索引作为额外列(,您也应该检查

最新更新