0

Any idea why I can't remove a data frame field called "Type"? Is is a keyword? Is there anyway to delete it. When I included the line that is commented-out below, I get the error:

AttributeError: 'DataFrame' object has no attribute 'Type'

This is an CSV download from Paypal that has many more fields than I need, so I'm trying to keep just a few of them.

del df['Shipping Address']
del df['Address Status']
del df['Gross']
del df['Fee']
del df['Status']
del df['TimeZone']
#del df['Type']  # it doesn't want to delete this one
del df['Currency']

Dump of all the fieldnames:

['Date' 'Time' 'TimeZone' 'Name' 'Type' 'Status' 'Currency' 'Gross' 'Fee'
 'Net' 'From Email Address' 'To Email Address' 'Transaction ID'
 'Shipping Address' 'Address Status' 'Item Title' 'Item ID'
 'Shipping and Handling Amount' 'Insurance Amount' 'Sales Tax'
 'Option 1 Name' 'Option 1 Value' 'Option 2 Name' 'Option 2 Value'
 'Reference Txn ID' 'Invoice Number' 'Custom Number' 'Quantity'
 'Receipt ID' 'Balance' 'Address Line 1'
 'Address Line 2/District/Neighborhood' 'Town/City'
 'State/Province/Region/County/Territory/Prefecture/Republic'
 'Zip/Postal Code' 'Country' 'Contact Phone Number' 'Subject' 'Note'
 'Country Code' 'Balance Impact']

reference: Delete a column from a Pandas DataFrame

NealWalters
  • 17,197
  • 42
  • 141
  • 251

1 Answers1

0

I've some so many places where reserved word cause problem, I jumped to a horrible conclusion.

Originally, I had something like this:

del df['Shipping Address']
del df['Address Status']
del df['Gross']
del df['Type']
del df['Fee']
del df['Status']
del df['TimeZone']
del df['Time']
df = df[ (df.Type != 'General Credit Card Deposit') &
         (df.Type != 'General Withdrawal') &
         (df.Type != 'Reversal of General Account Hold')
       ]

When I was trying some of the alternatives in the comments, I noticed that the error was not on the "del" statement, but on the use of df.Type after it had been deleted.

So the correction is rather obvious, I just need to delete the field after the places I use it:

del df['Shipping Address']
del df['Address Status']
del df['Gross']
del df['Fee']
del df['Status']
del df['TimeZone']
del df['Time']
df = df[ (df.Type != 'General Credit Card Deposit') &
         (df.Type != 'General Withdrawal') &
         (df.Type != 'Reversal of General Account Hold')
       ]
del df['Type']  # moved this line after the above 
NealWalters
  • 17,197
  • 42
  • 141
  • 251