iterrows无法正常工作-IndexError:单个位置索引器超出范围

时间:2019-02-12 18:37:37

标签: python pandas dataframe data-analysis data-cleaning

我正在尝试在两个数据框之间建立某种连接,并且我在两个数据框之间使用了递归操作,以获取索引和X列值,以便可以为lef表分配Y列值! / p>

我使用了以下内容:

for row_index,row in df.iterrows():
  for idx, r in pp.iterrows():
    if str(df.iloc[row_index]['nom_ens']) == str(pp.iloc[idx]['NOM_ENSEMBLE']) or (str(df.iloc[row_index]['nom_ens']) in str(pp.iloc[idx]['NOM_ENSEMBLE'])):
      df.iloc[row_index]["CODE_ENSEMBLE_IMMO"]=pp.iloc[idx]['CODE_ENSEMBLE_IMMO']

然后出现以下错误:

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:4: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  after removing the cwd from sys.path.
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-161-0895ce943ee3> in <module>()
      1 for idx, r in pp.iterrows():
      2   for row_index,row in df.iterrows():
----> 3     if str(df.iloc[row_index]['nom_ens']) == str(pp.iloc[idx]['NOM_ENSEMBLE']) or (str(df.iloc[row_index]['nom_ens']) in str(pp.iloc[idx]['NOM_ENSEMBLE'])):
      4       df.iloc[row_index]["CODE_ENSEMBLE_IMMO"]=pp.iloc[idx]['CODE_ENSEMBLE_IMMO']
      5 

/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in __getitem__(self, key)
   1371 
   1372             maybe_callable = com._apply_if_callable(key, self.obj)
-> 1373             return self._getitem_axis(maybe_callable, axis=axis)
   1374 
   1375     def _is_scalar_access(self, key):

/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in _getitem_axis(self, key, axis)
   1828 
   1829             # validate the location
-> 1830             self._is_valid_integer(key, axis)
   1831 
   1832             return self._get_loc(key, axis=axis)

/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in _is_valid_integer(self, key, axis)
   1711         l = len(ax)
   1712         if key >= l or key < -l:
-> 1713             raise IndexError("single positional indexer is out-of-bounds")
   1714         return True
   1715 

IndexError: single positional indexer is out-of-bounds

0 个答案:

没有答案