鱼C论坛

 找回密码
 立即注册
查看: 3223|回复: 3

求组,求助,求助,python pandas error!!!困扰致死

[复制链接]
发表于 2019-4-4 06:56:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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里是正常运行的。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-4 07:33:12 | 显示全部楼层

代码开始导入
import pandas as pd
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-4-4 08:18:02 | 显示全部楼层
casanava 发表于 2019-4-4 07:33
代码开始导入
import pandas as pd

这些模块我都导入啦

import pandas as pd
import numpy as np
import seaborn as sns
from datetime import date
from pandas_datareader.data import DataReader
import matplotlib.pyplot as plt
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-4 09:40:32 | 显示全部楼层
这里使用了read_excel()方法来读取excel,来看一个read_excel()这个方法的API,这里只截选一部分经常使用的参数:

pd.read_excel(io, sheet_name=0, header=0, names=None, index_col=None, usecols=None)
1
这里主要参数为io,sheet_name,header,usecols和names

io:excel文件,如果命名为中文,在python2.7中,需要使用decode()来解码成unicode字符串,例如: pd.read_excel('示例'.decode('utf-8))

sheet_name:返回指定的sheet,如果将sheet_name指定为None,则返回全表,如果需要返回多个表,可以将sheet_name指定为一个列表,例如['sheet1', 'sheet2']

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-5 04:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表