简体   繁体   中英

AttributeError: 'Pandas' object has no attribute 'Description Unit Price'

I am trying to import a data frame into a pdf. Here is the first 3 rows:

    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

I got the df.columns:

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

So clearly there is a column named 'Description Unit Price'?

There is the function that I have wrote (with the help of 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)

The error:

  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'

Can someone help? Is it because of spaces or mistake in using getattr (my first time using it) PS I dropped the NaN column

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

but u dont need iterate over columns, rows are like dict

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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