我正在使用Pandas导入一个似乎有用的小csv文件。当我查看导入的文件时,我会看到包含所有列标题和相应数据的漂亮格式的所有数据。我的问题是当我尝试运行命令报告时。(Tab键)例如我只看到该列表中的几个列标题。当我手动尝试运行report.RouterName时出现错误。我不看到的列标题是:RouterName,Router_Type,Switch_IP和CPU。如果我运行report.columns,我会看到列出的所有列标题。我究竟做错了什么?
In [1]: import pandas as pd
In [2]: report = pd.read_csv("/Users/iron-horse/Documents/Python_Files/Workbook1.csv")
In [3]: report
Out[3]:
RouterName Router_Type Switch_IP Software_Rev CPU \
0 BRPTCT0101 GX-550 10.10.50.7 09.01.01.20 NP
1 BRPTCT0106 CBX-500 (3 PS) 10.10.50.56 09.01.01.20 SP
2 BRPTCT0107 GX-550 10.10.50.35 09.01.01.20 NP
3 BRPTCT0102 CBX-500 (3 PS) 10.10.50.67 09.01.01.20 SP
Card_Fill Trunks LogicalPorts CircuitEndpoints Region Area
0 8 2 22 795 CT NE
1 13 6 74 905 CT NE
2 12 34 59 2039 CT NE
3 11 4 45 815 CT NE
In [4]: report.columns
Out[4]: Index([u'RouterName ', u'Router_Type ', u'Switch_IP ', u'Software_Rev', u'CPU ', u'Card_Fill', u'Trunks', u'LogicalPorts', u'CircuitEndpoints', u'Region', u'Area'], dtype='object')
In [5]: report.RouterName
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-a89b0f0e8073> in <module>()
----> 1 report.RouterName
/Users/iron-horse/anaconda/lib/python2.7/site-packages/pandas/core/generic.pyc in __getattr__(self, name)
1841 return self[name]
1842 raise AttributeError("'%s' object has no attribute '%s'" %
-> 1843 (type(self).__name__, name))
1844
1845 def __setattr__(self, name, value):
AttributeError: 'DataFrame' object has no attribute 'RouterName'
In [6]:
答案 0 :(得分:3)
它的间距。查看确切的列名称。它是什么?
解决方法是执行以下操作:
df.columns = map(str.strip, df.columns)
然后您可以访问列名df.RouterName
如果您想按原样执行,则必须执行df['RouterName ']