Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python if statement dictionary incompatible indexer with Series

This script :

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

gives this error :

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

But the script is working, meaning df.loc[x,'coutmed1'] takes the value that I want.

I don't understand what am I doing wrong ?

I think that the problem comes from this

dicoprix[df.loc[x,'medicament1']]
like image 841
salim Avatar asked Jun 14 '16 15:06

salim


1 Answers

This problem occurs when a key in the dict refers to more than one value !

like image 62
salim Avatar answered Oct 23 '22 15:10

salim