Python Pandas KeyError:'标签不在[索引]中'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46705389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:37:23  来源:igfitidea点击:

Python Pandas KeyError: 'the label is not in the [index]'

pythonlistpandasdataframe

提问by J. Doe

I have a duplicates_to_fetchdata frame of index :

我有一个duplicates_to_fetchindex 数据框:

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

And the following df_to_rearrangedata frame

以及以下df_to_rearrange数据框

        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]

I would like to have the following transformed data frame

我想要以下转换后的数据框

        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]    

However... when I drop the lines, the algorithm stops because of missing index

但是......当我删除这些行时,算法会因为缺少索引而停止

I did like

我喜欢

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)

The error is the following KeyError: 'the label [179] is not in the [index]'. Id there a more Pythonic way to do this ?

错误如下KeyError: 'the label [179] is not in the [index]'。有没有更 Pythonic 的方法来做到这一点?

回答by Zero

Here's another to way to do it. Use lookupfor groups, and aggthe columns to list

这是另一种方法。使用lookup团体,并agg在列list

In [78]: lookup = {v: i for i, x in enumerate(duplicates_to_fetch['Values']) for v in x}

In [79]: (df_to_rearrange.groupby(df_to_rearrange.index.to_series().map(lookup).fillna(''))
             .agg({'movie_name': lambda x: [v for v in x], 
                   'df_pname': lambda x: [a for v in x.values for a in v]})
             .reset_index(drop=True))
Out[79]:
  movie_name               df_pname
0    [A, Aa]          [mr a, mr aa]
1    [B, Bb]  [mr b, mr Bb, mr Bbb]
2        [D]                 [mr d]


Details

细节

In [959]: duplicates_to_fetch
Out[959]:
  mail_domaine  Values
0       @A.com  [0, 2]
1       @B.com  [1, 4]

In [960]: df_to_rearrange
Out[960]:
  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]
In [958]: lookup
Out[958]: {0: 0, 1: 1, 2: 0, 4: 1}

回答by Expired Brain

.reindex() as an alternative of .loc

.reindex() 作为 .loc 的替代品

http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#indexing-deprecate-loc-reindex-listlike

http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#indexing-deprecate-loc-reindex-listlike

df_to_rearrange.reindex([index_to_fetch], ["df_pname"]).append(df_to_rearrange.reindex([j], ["df_pname"]))

回答by J. Doe

One solution is

一种解决方案是

df_to_rearrange.drop(df_to_rearrange.index[np.where((df_to_rearrange.index==j))], inplace=True)