pandas python if语句字典与系列不兼容的索引器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37815837/
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 01:23:31  来源:igfitidea点击:

python if statement dictionary incompatible indexer with Series

pythonpandaspython-2.7

提问by salim

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.

但是脚本正在运行,意思df.loc[x,'coutmed1']是我想要的值。

I don't understand what am I doing wrong ?

我不明白我做错了什么?

I think that the problem comes from this

我认为问题来自于此

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

回答by salim

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

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

回答by ANKIT MORAL

Solution: Remove the duplicate indexes from the series (i.e. dicoprix) and keep them unique

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

You got it, the problem is in dicoprix[df.loc[x,'medicament1']]

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

There are duplicates in the indexes of the series dicoprix, which cannot be put as one value in the dataframe.

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

Below is the demonstration:

下面是演示:

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

Following command will run fine as 'a' is not the duplicate index in dum_serseries

以下命令将正常运行,因为“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

Error will occur when the command tries to insert two records from the series(as there are two records for the index bin dum_ser, to check run the command dum_ser['b']) into one value-space of the DataFrame. Refer below

当命令中尝试从系列中插入两个记录将出现错误(因为有两个记录为索引bdum_ser,以检查运行命令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

The above-written line of the code is the one of the iteration from the forloop i.e. for x=1

上面写的代码行是for循环中的迭代之一,即 for x=1

Solution: Remove the duplicate indexes from the series (i.e. dum_serhere) and keep them unique

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

回答by krish___na

Use indexing like this:

像这样使用索引:

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

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

It did work for me.

它确实对我有用。