AttributeError:将DF附加到列表,并使用openpyxl将这些DF的保存保存到聚合文件时,“ list”对象没有属性“ to_excel”

时间:2019-10-28 00:46:20

标签: python pandas openpyxl

当我想将最终的DataFrame保存到Excel文件时出现错误:

for filename in path.glob('**/*.xlsx'):
[...]
[... omitted code, will share, if interest exists]
[...]
    print('Processing : ' + str(filename))
    try:
        data = pd.read_excel(filename, sheet_name='Main Sheet', header=None)
        new_row = pd.DataFrame([[str(filename), str(now)]],
                               index=[0])
        # simply concatenate both dataframes
        data = pd.concat([new_row, data]).reset_index(drop=True, inplace=True)

        appended_data.append(data)
        appended_data = pd.concat(appended_data, sort=False, ignore_index=True)
    except Exception as e:
        print(e)
        print('Couldn\'t process ' + str(filename) + ' ! ')
    copy('C:\\Users\\**YOU**\\' + str(filename), (os.path.expanduser(
        '~/') + '\\**CLOUD'))
    os.remove('C:\\Users\\**YOU**\\' + str(filename))

except Exception as e:
    print('Error! Error!: ' + str(e) + str(e.args))

循环后:

appended_data.to_excel('appended.xlsx')
book = load_workbook('appended2.xlsx')
writer = pd.ExcelWriter('appended2.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = {ws.title: ws for ws in book.worksheets}
startrow = writer.sheets['Sheet1'].max_row
appended_data.to_excel(writer, startrow=startrow, index=False, header=False)
writer.save()

“ AttributeError:'list'对象没有属性'to_excel'” 发生在这里的倒数第二行。 我很困惑,因为代码在对循环进行一些“改进”之前就可以工作了。 列表如何变成数据框? 当我尝试一个简单的     df = pd.DataFrame(appended_data) 我收到“所有传递的对象均为无”


一些背景信息: 数据框如下所示:

标题

Eaten this month    Ordered Self-made   Eaten out

Pizza   20  5   7   8
Pasta   10  1   8   1
Sushi   5       0   N/A
Chinese 15  14  1   N/A

标题被删除,并且汇总的数据已写入名称和日期 附加数据(最终结果):

Wight    2019/10/28
Pizza   20  5   7   8
Pasta   10  1   8   1
Sushi   5       0   N/A
Chinese 15  14  1   N/A
Olufsson    2019/10/27
Pizza   20  5   7   8
Pasta   10  1   8   1
Sushi   5       0   N/A
Chinese 15  14  1   N/A

1 个答案:

答案 0 :(得分:0)

我走近了,我不得不使用append而不是concat。我也将操作移到其中的循环之后。

data = new_row.append(data, ignore_index=True)
            appended_data.append(data)
            try:
                new_append = appended_data.append(data)
            except:
                print('Could\'nt append multiple df\'s')
            try:
                appended_data = pd.concat(new_append.reset_index(drop=True), sort=False, ignore_index=True, axis=1)
            except:
                pass
            df = pd.DataFrame(appended_data)
            df.to_excel('appended.xlsx')
            book = load_workbook('appended2.xlsx')
            writer = pd.ExcelWriter('appended2.xlsx', engine='openpyxl')
            writer.book = book
            writer.sheets = {ws.title: ws for ws in book.worksheets}
            startrow = writer.sheets['Sheet1'].max_row
            df.to_excel(writer, startrow=startrow, index=False, header=False)
            writer.save()

我还有一个问题: 最终结果中的数据帧被写入一个单元格,这似乎很奇怪。 但我想将从这里开始管理索引可能有问题...