键不在字典中,产生KeyError 1

时间:2020-01-18 19:22:44

标签: python pandas

我收到KeyError 1,不确定为什么会重现此错误。

for i in range(num_clusters):
    if i != 0:
        words ="Cluster %d words: " % i
        print (words)

    for j, ind in enumerate(order_centroids[i, :10]):
        if (j == 0):
            words1= '%s' % vocab_df.loc[terms[ind].split(' ')].values.tolist()[0][0]
            print (words1)
        else:
           words2= '%s' % vocab_df.loc[terms[ind].split(' ')].values.tolist()[0][0]
           print (words2)

    titles="Cluster %d titles: " % i
    print (titles)
    for j, title in enumerate(df.loc[i]['title'].values.tolist()):
        if (j == 0):
            title1= '%s' % title
            print(title1)
        else:
            title2= ', %s' % title
            print (title2)

跟踪:

File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\indexes\base.py" in get_loc 2897.
    return self._engine.get_loc(key)
File "pandas\_libs\index.pyx" in pandas._libs.index.IndexEngine.get_loc 107.
    <source code not available>
File "pandas\_libs\index.pyx" in pandas._libs.index.IndexEngine.get_loc 131.
    <source code not available>
File "pandas\_libs\hashtable_class_helper.pxi" in pandas._libs.hashtable.Int64HashTable.get_item 992.
    <source code not available>
File "pandas\_libs\hashtable_class_helper.pxi" in pandas._libs.hashtable.Int64HashTable.get_item 998.
    <source code not available>

During handling of the above exception (1), another exception occurred:
File "C:\Users\User\Anaconda3\lib\site-packages\django\core\handlers\exception.py" in inner 34.
    response = get_response(request)
File "C:\Users\User\Anaconda3\lib\site-packages\django\core\handlers\base.py" in _get_response 115.
    response = self.process_exception_by_middleware(e, request)
File "C:\Users\User\Anaconda3\lib\site-packages\django\core\handlers\base.py" in _get_response 113.
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\User\waqaf\waqaf\views.py" in output4 638.
    for j, title in enumerate(df.loc[i]['title'].values.tolist()):
File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\indexing.py" in __getitem__ 1424.
    return self._getitem_axis(maybe_callable, axis=axis)
File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\indexing.py" in _getitem_axis 1850.
    return self._get_label(key, axis=axis)
File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\indexing.py" in _get_label 160.
    return self.obj._xs(label, axis=axis)
File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\generic.py" in xs 3735.
    loc, new_index = self.index.get_loc_level(key, drop_level=drop_level)
File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\indexes\multi.py" in get_loc_level 2856.
    indexer = self._get_level_indexer(key, level=level)
File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\indexes\multi.py" in _get_level_indexer 2939.
    code = level_index.get_loc(key)
File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\indexes\base.py" in get_loc 2899.
    return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx" in pandas._libs.index.IndexEngine.get_loc 107.
    <source code not available>
File "pandas\_libs\index.pyx" in pandas._libs.index.IndexEngine.get_loc 131.
    <source code not available>
File "pandas\_libs\hashtable_class_helper.pxi" in pandas._libs.hashtable.Int64HashTable.get_item 992.
    <source code not available>
File "pandas\_libs\hashtable_class_helper.pxi" in pandas._libs.hashtable.Int64HashTable.get_item 998.
    <source code not available>
  Exception Type: KeyError at /output4
  Exception Value: 1

我想要的输出是:

Clusters:
----------------------------------------
Cluster 0 words: mathematics, accounting, engineering, understanding, finance, book, business, edition, basic, courses
Cluster 0 titles: Discrete Mathematics and Its Applications.txt, financial accounting.txt, First Engineering mathematics.txt, Foundations of Finance.txt, Linear Algebra.txt, quantitative business analysis for uitm.txt
Cluster 1 words: programming, c++, computer, c++, language, book, science, extensibility, software, computer
Cluster 1 titles: a gift of fire.txt, C++ for Dummies.txt, C++ How To Program.txt, Introduction to C++ Programming.txt
Cluster 2 words: web, applications, internet, simple, book, databases, programming, applications, building, techniques
Cluster 2 titles: Internet & World Wide Web .txt, PHP & MYSQL for Dummies.txt

任何人都可以解释为什么会这样吗?我正在寻找解决方案,但仍然不知道如何解决此问题。看来我无法访问词典的标题为title的关键字(不确定是否正确)。

数据示例:biochemistry.txt

- welcome to the study of the chemistry of life. as you venture into this exiting and dynamic discipline, you'll discover many new and wonderful things. you'll learn how some enzymes can catalyze chemical reactions at speeds close to theoretical limits- reactions that would otherwise occur only at imperceptibly low rates. you'll learn about the forces that maintain biomolecular structure and how even some of the weakest of those forces make life possible. you'll also learn how biochemistry has thousands of applications in day-to-day lide- in medicine, drug design, nutrition, forensic science, agriculture, and manufacturing. in short, you'll begin a journey of discovery about how chemistry makes life both possible and better.

0 个答案:

没有答案