python if语句字典与Series不兼容的索引器

时间:2016-06-14 15:08:59

标签: python-2.7

这个脚本:

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']]

3 个答案:

答案 0 :(得分:4)

当dict中的某个键引用多个值时会发生此问题!

答案 1 :(得分:1)

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

您明白了,问题出在dicoprix[df.loc[x,'medicament1']]

dicoprix系列索引中有重复项,不能将其作为一个值放入数据框中。

下面是演示:

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
  

当命令尝试从序列中插入两条记录时会发生错误(因为在b中有两个索引dum_ser的记录,以检查运行命令dum_ser['b'])到DataFrame的一个值空间。请参阅下面

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),并使其保持唯一性

答案 2 :(得分:0)

使用这样的索引编制:

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

它确实对我有用。