此脚本:
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']]
当dict中的键指的是一个以上的值时,就会发生此问题!
解决方案:删除系列中的重复索引(即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
命令试图从该系列中插入两个记录时(因为
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:ProgramDataAnaconda3libsite-packagespandascoreindexing.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:ProgramDataAnaconda3libsite-packagespandascoreindexing.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:ProgramDataAnaconda3libsite-packagespandascoreindexing.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
此处),并保持它们唯一
我也有同样的问题,
for i, (result, status) in enumerate(results):
df.at[i, 'response'] = result
我的理解,这个错误可能意味着您尝试分配的值的 type
与dataframe中的列 type
匹配
在我的情况下,result
是dict
,因为它响应发布请求
解决方案很容易 - 铸型到字符串:
for i, (result, status) in enumerate(results):
df.at[i, 'response'] = str(result)
使用这样的索引:
dicoprix [df.loc [x,'presenament1']] [0]
它确实对我有用。