python if语句字典与Series不兼容的索引器 [英] python if statement dictionary incompatible indexer with Series

查看:908
本文介绍了python if语句字典与Series不兼容的索引器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此脚本:

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']采用了我想要的值.

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 ?

我认为问题来自此

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

推荐答案

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

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

这篇关于python if语句字典与Series不兼容的索引器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆