|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
listings=pd.read_excel('listings.xlsx')#这是一个有三个sheet的excel表格,这个表格的三个sheet名称就是exchanges里的那三个
# Create the list exchanges
exchanges = [ 'amex','nasdaq', 'nyse']
# Iterate over exchanges then plot and show result
for exchange in exchanges:
sectors = listings[exchange].Sector.value_counts()
#.value_counts():count of each unique value
# Sort in descending order and plot
sectors.sort_values(ascending=False).plot(kind='bar')
# Show the plot
plt.show()
结果出现这样的错误:
Traceback (most recent call last):
File "/Users/apple/Library/Python/3.7/lib/python/site-packages/pandas/core/indexes/base.py", line 2657, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'amex'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/apple/Desktop/python_work/data camp/import and manage financila data/import and manage financial data 2.py", line 18, in <module>
sectors = listings[exchange].Sector.value_counts()
File "/Users/apple/Library/Python/3.7/lib/python/site-packages/pandas/core/frame.py", line 2927, in __getitem__
indexer = self.columns.get_loc(key)
File "/Users/apple/Library/Python/3.7/lib/python/site-packages/pandas/core/indexes/base.py", line 2659, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'amex'
[Finished in 2.7s with exit code 1]
求解求解,我怀疑是导入文件dataframe的问题,因为除了第一个导入excel文件的语句,下面的语句在Datacamp的教学ipython shell里是正常运行的。
|
|