redeemer_liu 发表于 2022-9-15 09:52:41

frame.append如何替代

Warning (from warnings module):
File "C:/Users/User/AppData/Local/Programs/Python/Python39/test_program/故障时间分类统计.py", line 11
    table = table.append(data, ignore_index=True)
FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.

import xlwings as xw
import pandas as pd
app = xw.App(visible=False, add_book=False)
workbook = app.books.open('C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python39\\test_program\\三冷电气周故障汇总2022.xlsx')
worksheets = workbook.sheets
table = pd.DataFrame()
for i, j in enumerate(worksheets):
    data = j.range('B2').options(pd.DataFrame, header=1, index=False, expand='table').value
    data = data.reindex(columns=['机组', '日期', '设备停机', '故障名称'])
    table = table.append(data, ignore_index=True)
table = table.groupby('机组')


那提示的这个问题,如何替代?初学者求教

ba21 发表于 2022-9-15 13:49:54

提示很清楚了

Use pandas.concat insteadpandas.concat

redeemer_liu 发表于 2022-9-15 16:55:13

=============================== RESTART: C:/Users/User/AppData/Local/Programs/Python/Python39/test_program/故障时间分类统计.py ==============================
Traceback (most recent call last):
File "C:/Users/User/AppData/Local/Programs/Python/Python39/test_program/故障时间分类统计.py", line 10, in <module>
    table = pd.contact(data, ignore_index=True)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\__init__.py", line 261, in __getattr__
    raise AttributeError(f"module 'pandas' has no attribute '{name}'")
AttributeError: module 'pandas' has no attribute 'contact'

还是报错

hrpzcf 发表于 2022-9-16 14:27:22

pandas.concat
一个字母都不能错!!!

redeemer_liu 发表于 2022-9-16 16:32:19

hrpzcf 发表于 2022-9-16 14:27
pandas.concat
一个字母都不能错!!!

我知道我错了,但还是一样

redeemer_liu 发表于 2022-9-16 16:35:06

Traceback (most recent call last):
File "C:/Users/User/AppData/Local/Programs/Python/Python39/test_program/故障时间分类统计.py", line 10, in <module>
    table = pd.concat(data, ignore_index=True)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\reshape\concat.py", line 347, in concat
    op = _Concatenator(
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\reshape\concat.py", line 382, in __init__
    raise TypeError(
TypeError: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame"

redeemer_liu 发表于 2022-9-16 16:37:24

import xlwings as xw
import pandas as pd
app = xw.App(visible=False, add_book=False)
workbook = app.books.open('C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python39\\test_program\\三冷电气周故障汇总2022.xlsx')
worksheets = workbook.sheets
table = pd.DataFrame()
for i, j in enumerate(worksheets):
    data = j.range('B2').options(pd.DataFrame, header=1, index=False, expand='table').value
    data = data.reindex(columns=['机组', '日期', '设备停机', '故障名称'])
    table = pd.concat(data, ignore_index=True)
table = table.groupby('机组')
new_workbook = xw.books.add()
for idx, group in table:
    new_worksheet = new_workbook.sheets.add(idx)
    new_worksheet['B2'].options(index=False).value = group
    last_cell = new_worksheet['B2'].expand('table').last_cell
    last_row = last_cell.row
    last_column = last_cell.column
    last_column_letter = chr(64 + last_column)
    sum_cell_name = f'{last_column_letter}{last_row + 1}'
    sum_last_row_name = f'{last_column_letter}{last_row}'
    formula = f'=SUM({last_column_letter}2:{sum_last_row_name})'
    new_worksheet.formula = formula
    new_worksheet.autofit()
new_workbook.save('C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python39\\test_program\\电气周故障汇总2022.xlsx')
app.quit()

源程序,求指教,初学,想把表格弄弄好

redeemer_liu 发表于 2022-9-16 16:38:26

这个是书上的程序,我只是把路径和表格替换了一下
页: [1]
查看完整版本: frame.append如何替代