这很棘手,但我需要你的帮助。总之,我负责数据的同事离开了,他的Python请求中有一个未解决的错误。我对这种语言有接近0的知识,而且我没有编写这些Python请求,所以我无法解决这个问题。以下是代码:
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)
。以下是错误:
---------------------------------------------------------------------------
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:,:“,但是它没有解决这个问题。
提前一吨谢谢!
发布于 2022-08-05 09:25:37
问题在于您的churn_calculation
方法
----> 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列。
https://stackoverflow.com/questions/73246897
复制相似问题