微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

发生了什么事? 谢谢。

原文地址:https://www.jb51.cc/python/3191857.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐