Python Pandas KeyError: 'l'étiquette n'est pas dans le [index]'

J'ai un duplicates_to_fetch bloc de données d'index :

            mail_domaine         Values
0                 @A.com         [0, 2]
1                 @B.com         [1, 4]

Et suivants df_to_rearrange bloc de données

        movie_name              df_pname 
0                A                [mr a]   
1                B                [mr b]   
2               Aa               [mr aa]   
3                D                [mr d]   
4               Bb       [mr Bb, mr Bbb]
5                E                [mr e]

Je voudrais avoir les données transformées cadre

        movie_name                    df_pname 
0          [A, Aa]               [mr a, mr aa]   
1          [B, Bb]       [mr b, mr Bb, mr Bbb]   
3              [D]                      [mr d] 
5              [E]                      [mr e]    

Cependant... quand je baisse les lignes, l'algorithme s'arrête en raison de l'absence d'index

J'ai fait comme

for i in range(0,len(druplicates_to_fetch)):
      mylist = duplicates_to_fetch.loc[i,"Values"]
      index_to_fetch_on = mylist[0]

      # rearrange mylist (which can have >2 values)
      mylist = [myindex for myindex in mylist if myindex != index_to_fetch_on]

      for j in mylist:
            df_to_rearrange.loc[index_to_fetch, "df_pname"].append(df_to_rearrange.loc[j, "df_pname"])
            df_to_rearrange.drop(df_to_rearrange.index[j], inplace=True)

L'erreur est la suivante KeyError: 'the label [179] is not in the [index]'. Id il y a plus Pythonic façon de le faire ?

  • Itération avec for i in range(0,len(duplicates_to_fetch)): mylist = duplicates_to_fetch.loc[i,"Values"] est maladroit. De mieux à faire: for i, duplicate_row in duplicates_to_fetch.iterrows()
  • Aussi, mylist = [ix for ix in mylist if ix != index_to_fetch_on] ressemble à un simple drop() opération.
InformationsquelleAutor J. Doe | 2017-10-12