pandas AttributeError: 'DataFrame' 对象没有属性 'Address'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40875557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:32:42  来源:igfitidea点击:

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

pythonpython-2.7csvpandas

提问by Jason Smith

I am using Pandas to insert a title() into the Address column in my csv file so I can directly accurate my csv file without making a temp csv file but it keeps throwing out an error. Please provide your punctilious advice.

我正在使用 Pandas 将 title() 插入到我的 csv 文件的 Address 列中,这样我就可以直接精确我的 csv 文件而无需制作临时 csv 文件,但它不断抛出错误。请提供您的中肯建议。

import pandas as pd

df = pd.read_fwf('C:\Users\Admissions.csv')
df.Address = df.Address.apply(lambda x: x.title())
df.to_csv('C:\Users\Admissions.csv', index=False, sep='\t')

Error:

错误:

Traceback (most recent call last):
  File "C:\Users\Addressupdate.py", line 23, in <module>
    df.Address = df.Address.apply(lambda x: x.title())
  File "C:\Python27\lib\site-packages\pandas\core\generic.py", line 2744, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'Address'

Error:

错误:

df = pd.read_fwf('C:\Users\Admissions.csv')
df['Address'] = df['Address'].str.title()
df.to_csv('C:\Users\Admissions.csv', index=False, sep='\t')

File "pandas/index.pyx", line 139, in pandas.index.IndexEngine.get_loc (pandas\index.c:4160)
  File "pandas/index.pyx", line 161, in pandas.index.IndexEngine.get_loc (pandas\index.c:4024)
  File "pandas/src/hashtable_class_helper.pxi", line 732, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13161)
  File "pandas/src/hashtable_class_helper.pxi", line 740, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13115)
KeyError: 'Address'

回答by AlexG

Based on the comments above, you have some strange characters in the column titles in Admissions.csv. What if you rename the columns in Python immediately after reading it?

根据上面的评论,您在Admissions.csv 的列标题中有一些奇怪的字符。如果您在阅读 Python 后立即重命名列会怎样?

df.columns = ["Permit Number","Address","Street Name","Applicant Name","Contractor Name","SITE_SUBDIVISION","RECORDID"]