Python-KeyError:在一个简单的循环中是1吗?

我有一个数据框,它代表组合的名称,表征这些组合的人员以及每个组合的平均分数。从上到下遍历数据框,我想提取组合,以便一个组合与另一个组合没有同一个人。具体而言,不应以多种组合形式存在一个人。

尽管不是很优雅,但我设法创建了该算法。不幸的是,当我在数据框上应用过滤器时,我遇到了一个错误,该错误使我很困惑。这是说明我的示例的可重复代码

import pandas as pd
import numpy as np
 
#I initialize my lists
compo_df = []
group_df = []
 
#I create my dataframe
df = pd.DataFrame({'nom_combinaison': ['Compo1','Compo2','Compo3','Compo4','Compo5','Compo6','Compo7','Compo8'],'prenom': ["[Personne3,Personne5,Personne6]","[Personne3,Personne4,Personne11]","[Personne9,Personne11,Personne12]","[Personne8,Personne9,"[Personne1,Personne8]",Personne3,Personne4]",Personne6,Personne10]"],'moyenne': np.random.randn(8)})
 
#HERE,THE LINE THAT MAKES MY PROGRAM BUILD. THE LATTER WORKS OK IF DELETED
df = df.loc[(df.moyenne > 0.2)]
 
#By default,I add the first element of combinations and people to my lists
compo_df.append(df["nom_combinaison"][0])
group_df.append(df["prenom"][0])
 
a_remplir = []
 
for i in range(0,len(df)): #I'm browsing my dataframe
    if len(set(df["prenom"][i]) & set(a_remplir)) == 0: #If the list "a_fill" does not contain an element similar to df ["first name"] [i]:
        print("combi %s : valeur %s"%(df["nom_combinaison"][i],df["prenom"][i])) #To help me,I display the elements
        compo_df.append(df["nom_combinaison"][i]) #I add the name of the combination to my list ...
        group_df.append(df["prenom"][i])# .... as well as the people
        print("Je met")
        for value in df["prenom"][i]: #And I add to "a_fill" the values ​​of the list of df ["firstname"] [i] to avoid having groups with similar people between them
            a_remplir.append(value)
            print(value)
    else:
        print("combi %s : valeur %s"%(df["nom_combinaison"][i],df["prenom"][i]))
        print("similitude : je rejette")
        print("\n")
 
print(list(set(compo_df)))
print((group_df))

返回的错误是:

combi Combinaison_1 : valeur ['Personne3','Personne4','Personne11']
Je met
Personne3
Personne4
Personne11
 
Traceback (most recent call last):
  File "delete.py",line 20,in <module>
    if len(set(df["prenom"][i]) & set(a_remplir)) == 0: #Si la liste "a_remplir"  ne contient pas d'élément semblable à df["prenom"][i] : 
  File "C:\ProgramData\Miniconda3\lib\site-packages\pandas\core\series.py",line 871,in __getitem__
    result = self.index.get_value(self,key)
  File "C:\ProgramData\Miniconda3\lib\site-packages\pandas\core\indexes\base.py",line 4405,in get_value
    return self._engine.get_value(s,k,tz=getattr(series.dtype,"tz",None))
  File "pandas\_libs\index.pyx",line 80,in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx",line 90,line 138,in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi",line 998,in pandas._libs.hashtable.Int64HashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi",line 1005,in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 1

发生了什么事? 谢谢。

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...