【问题标题】:python if statement dictionary incompatible indexer with Seriespython if语句字典与Series不兼容的索引器
【发布时间】:2016-10-15 09:52:57
【问题描述】:

这个脚本:

for x in df.index: 
    if df.loc[x,'medicament1'] in dicoprix:
        df.loc[x,'coutmed1'] = dicoprix[df.loc[x,'medicament1']]

给出这个错误:

File "<ipython-input-35-097fdb2220b8>", line 3, in <module>
    df.loc[x,'coutmed1'] = dicoprix[df.loc[x,'medicament1']]

  File "//anaconda/lib/python2.7/site-packages/pandas/core/indexing.py", line 115, in __setitem__
    self._setitem_with_indexer(indexer, value)

  File "//anaconda/lib/python2.7/site-packages/pandas/core/indexing.py", line 346, in _setitem_with_indexer
    value = self._align_series(indexer, value)

  File "//anaconda/lib/python2.7/site-packages/pandas/core/indexing.py", line 613, in _align_series
    raise ValueError('Incompatible indexer with Series')

ValueError: Incompatible indexer with Series

但脚本正在运行,这意味着 df.loc[x,'coutmed1'] 采用了我想要的值。

我不明白我做错了什么?

我认为问题出在这个

dicoprix[df.loc[x,'medicament1']]

【问题讨论】:

    标签: python pandas python-2.7


    【解决方案1】:

    解决方案:从系列中删除重复的索引(即 dicoprix)并保持它们的唯一性

    知道了,问题出在dicoprix[df.loc[x,'medicament1']]

    dicoprix系列的索引有重复,不能作为一个值放在dataframe中。

    下面是演示:

    In [1]: 
    import pandas as pd
    dum_ser = pd.Series(index=['a','b','b','c'], data=['apple', 'balloon', 'ball', 'cat' ])
    
    [Out 1]
    a      apple
    b    balloon
    b       ball
    c        cat
    dtype: object
    
    
    In [2]: 
    df = pd.DataFrame({'letter':['a','b','c','d'], 'full_form':['aley', 'byue', 'case', 'cible']}, index=[0,1,2,3])
    df
    
    Out [2]:
        letter  full_form
    0   a   aley
    1   b   byue
    2   c   case
    3   d   cible
    

    以下命令将正常运行,因为 'a' 不是 dum_ser 系列中的重复索引

    In [3]: 
    df.loc[0,'full_form'] = dum_ser['a']
    df
    
    Out [3]:
        letter  full_form
    0   a   apple
    1   b   byue
    2   c   case
    3   d   apple
    

    当命令尝试将序列中的两条记录(因为dum_ser 中的索引b 有两条记录,请检查运行命令dum_ser['b'])到数据框。参考下面

    In [4]:
    df.loc[1,'full_form'] = dum_ser['b']
    
    Out [4]:
        ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-4-af11b9b3a776> in <module>()
    ----> 1 df.loc['b','full_form'] = dum_ser['b']
    
    C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py in __setitem__(self, key, value)
        187             key = com._apply_if_callable(key, self.obj)
        188         indexer = self._get_setitem_indexer(key)
    --> 189         self._setitem_with_indexer(indexer, value)
        190 
        191     def _validate_key(self, key, axis):
    
    C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py in _setitem_with_indexer(self, indexer, value)
        635                 # setting for extensionarrays that store dicts. Need to decide
        636                 # if it's worth supporting that.
    --> 637                 value = self._align_series(indexer, Series(value))
        638 
        639             elif isinstance(value, ABCDataFrame):
    
    C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py in _align_series(self, indexer, ser, multiindex_indexer)
        775             return ser.reindex(ax)._values
        776 
    --> 777         raise ValueError('Incompatible indexer with Series')
        778 
        779     def _align_frame(self, indexer, df):
    
    ValueError: Incompatible indexer with Series
    

    上面编写的代码行是来自for 循环的迭代之一,即对于 x=1

    解决方案:从系列中删除重复的索引(即此处为dum_ser)并保持其唯一性

    【讨论】:

    • 我遇到了同样的问题,这对我有用,我只是运行df = df[df.index.drop_duplicates()] 来删除重复的索引。但重要的是要注意,在我的情况下,删除的行并不重要。
    【解决方案2】:

    像这样使用索引:

    dicoprix[df.loc[x,'medicament1']][0]

    它确实对我有用。

    【讨论】:

      【解决方案3】:

      当 dict 中的键引用多个值时会出现此问题!

      【讨论】:

      • 如果错误消息能准确说明,那就太好了。
      猜你喜欢
      • 2020-05-17
      • 2021-09-06
      • 1970-01-01
      • 2016-11-28
      • 1970-01-01
      • 2012-06-06
      • 2013-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多