首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >IndexError:单位置索引器超出了范围(iloc[1:,:])

IndexError:单位置索引器超出了范围(iloc[1:,:])
EN

Stack Overflow用户
提问于 2022-08-05 08:34:36
回答 1查看 141关注 0票数 0

这很棘手,但我需要你的帮助。总之,我负责数据的同事离开了,他的Python请求中有一个未解决的错误。我对这种语言有接近0的知识,而且我没有编写这些Python请求,所以我无法解决这个问题。以下是代码:

代码语言:javascript
运行
复制
churn_list = []
for i in Version:
  churn_temp = churn_data[churn_data['Version']==i].sort_values('Level').reset_index()
  churn_temp = churn_temp.iloc[1: , :]
  **churn_temp = churn_calculation(churn_temp)**

  # Plotting Churn
  ##
  import plotly.graph_objects as go
  fig = go.Figure()
  fig.add_trace(go.Bar(x=churn_temp[churn_temp['Level']<15]['Level'], 
                      y=churn_temp[churn_temp['Level']<15]['Churn_perc'],
                      name="drop off in %"))
  fig.add_trace(go.Scatter(x=churn_temp[churn_temp['Level']<15]['Level'], 
                          y=churn_temp[churn_temp['Level']<15]['Cumulative_Churn_perc'],
                          name="Cumulative drop off in %"))
  fig.update_layout(
      autosize=False,
      width=1000,
      height=500,
      template='simple_white',
      title_text='Churn Visualisation for version '+i,
      yaxis_title="drop off in %",
      xaxis_title="Level",
      title_x=0.5)
  
  fig.show()
  churn_list.append(churn_temp)

这是一个基于BigQuery请求计算搅动的代码。它在最初的几天里运作得很好,然后意外地发生了。错误发生在churn_temp = churn_calculation(churn_temp)。以下是错误:

代码语言:javascript
运行
复制
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-73-ddc2b4335820> in <module>()
      3   churn_temp = churn_data[churn_data['Version']==i].sort_values('Level').reset_index()
      4   churn_temp = churn_temp.iloc[1: , :]
----> 5   churn_temp = churn_calculation(churn_temp)
      6 
      7   # Plotting Churn

5 frames
<ipython-input-56-4b23d0a07bff> in churn_calculation(df)
      4   df.loc[:,'Cumulative Churn']=df.loc[:,'Chrun'].expanding(1).sum()
      5   df.loc[:,'Churn_perc']=round((df.loc[:,'Chrun']/df.loc[:,'Players'])*100,2)
----> 6   df.loc[:,'Cumulative_Churn_perc']=round((df.loc[:,'Cumulative Churn']/df.iloc[0,3])*100,2)
      7   return df

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in __getitem__(self, key)
    923                 with suppress(KeyError, IndexError):
    924                     return self.obj._get_value(*key, takeable=self._takeable)
--> 925             return self._getitem_tuple(key)
    926         else:
    927             # we by definition only have the 0th axis

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in _getitem_tuple(self, tup)
   1504     def _getitem_tuple(self, tup: tuple):
   1505 
-> 1506         self._has_valid_tuple(tup)
   1507         with suppress(IndexingError):
   1508             return self._getitem_lowerdim(tup)

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in _has_valid_tuple(self, key)
    752         for i, k in enumerate(key):
    753             try:
--> 754                 self._validate_key(k, i)
    755             except ValueError as err:
    756                 raise ValueError(

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in _validate_key(self, key, axis)
   1407             return
   1408         elif is_integer(key):
-> 1409             self._validate_integer(key, axis)
   1410         elif isinstance(key, tuple):
   1411             # a tuple should already have been caught by this point

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in _validate_integer(self, key, axis)
   1498         len_axis = len(self.obj._get_axis(axis))
   1499         if key >= len_axis or key < -len_axis:
-> 1500             raise IndexError("single positional indexer is out-of-bounds")
   1501 
   1502     # -------------------------------------------------------------------

IndexError: single positional indexer is out-of-bounds

有人能在这件事上帮我吗?我搜索了类似的问题,但并不完全一样,所以我没有找到解决方案。我试图用数据库的确切列数和行数替换当前的"churn_temp.iloc1:,:“,但是它没有解决这个问题。

提前一吨谢谢!

EN

回答 1

Stack Overflow用户

发布于 2022-08-05 09:25:37

问题在于您的churn_calculation方法

代码语言:javascript
运行
复制
----> 6   df.loc[:,'Cumulative_Churn_perc']=round((df.loc[:,'Cumulative Churn']/df.iloc[0,3])*100,2)

在这里使用df.iloc[0,3]iloc索引从0开始,问题可能是您的df是空的,或者df没有4列。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73246897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档