无法删除名为 "Type" 的 Pandas 数据帧字段



为什么我不能删除一个名为"键入"?是关键字吗?有没有删除它。当我包括下面评论的行时,我得到了错误:

属性错误:"DataFrame"对象没有属性"Type"

这是一个从Paypal下载的CSV文件,它的字段比我需要的多得多,所以我尽量只保留其中的一些字段。

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']

所有字段名称的转储:

['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']

参考:从Pandas DataFrame 中删除一列

我有很多地方的保留字会引起问题,我得出了一个可怕的结论。

最初,我有这样的东西:

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')
]

当我尝试评论中的一些替代方案时,我注意到错误不在";del";语句,但是关于df的使用。在删除后键入。

所以修正是相当明显的,我只需要删除我使用它的地方之后的字段:

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 

相关内容

最新更新