繁体   English   中英

AttributeError:“熊猫”对象没有属性“描述单价”

[英]AttributeError: 'Pandas' object has no attribute 'Description Unit Price'

我正在尝试将数据框导入 pdf。 这是前 3 行:

    Description Unit Price Quantity  Unnamed: 0        VAT %    Amount       VAT
0                                                                                                 Ground Handling Fee 2,000.00    1 occ         NaN          15%  2,000.00    300.00
1                                                                                               After Hours Surcharge 1,000.00    1 occ         NaN          15%  1,000.00    150.00
2                                                                                     VIP Lounge child Departure Access 125.00    2 pax         NaN          15%    250.00     37.50

我得到了 df.columns:

Index(['Description Unit Price', 'Quantity', 'VAT %', 'Amount', 'VAT'], dtype='object')

很明显有一个名为“描述单价”的列?

我写了一个函数(在 yt 的帮助下):

def df_to_pdf(pdf, df):
    table_cell_width = 25
    table_cell_height = 6
    pdf.set_font('Arial', 'B', 8)
    df = df.reset_index()
    cols = df.columns
    for col in cols:
        pdf.cell(table_cell_width, table_cell_height, col, align='C', border=1)
        pdf.ln(table_cell_height)
        pdf.set_font('Arial', '', 6)
        for row in df.itertuples():
            for col in cols:
                value = str(getattr(row, col))
                pdf.cell(table_cell_width, table_cell_height, value, align='C', border=1)
                pdf.ln(table_cell_height)

pdf.ln(20)
df_to_pdf(pdf, df)

错误:

  File "C:\Users\User\PycharmProjects\pythonProject1\invoice_model\result.py", line 300, in df_to_pdf
    value = str(getattr(row, col))
AttributeError: 'Pandas' object has no attribute 'Description Unit Price'

有人可以帮忙吗? 是因为空格还是使用getattr的错误(我第一次使用它)PS我删除了NaN列

in value = str(getattr(row, col))使用value = str(row[col])

但你不需要遍历列,行就像 dict

for row in df.itertuples():
    for col in row:
        print(col, row[col])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM