键错误('%s不在索引'%objarr [mask])

时间:2017-05-09 18:50:45

标签: python pandas sqlalchemy

正在修复数据框 - 这就是错误所在

def group_analytics(df):
    """
    *prints various stats to the console for a dataframe created in the 
    'group_trips' function*
    """
df_fp = df[['freeplay_redeemed','cash_in', 'actual_win', 'theo_win']]

df_fp['cash_to_fp'] = df_fp['cash_in'].astype(float) / df_fp['freeplay_redeemed'].astype(float) 
df_fp['w_above_fp'] = df_fp['actual_win'].astype(float) - df_fp['freeplay_redeemed'].astype(float) 
df_fp['tw_above_fp'] = df_fp['theo_win'].astype(float) - df_fp['freeplay_redeemed'].astype(float) 
df_fp = df_fp.replace([np.inf, -np.inf], np.nan).dropna()
df_fp['amt_above_fp'] = df_fp[['w_above_fp', 'tw_above_fp']].max(axis=1)

print('\nCash in :',df_fp['cash_in'].astype(float).sum())
print ('\n# trips: ', df_fp['freeplay_redeemed'].count())
print ('\nActual Win: ', df_fp['actual_win'].sum())
print ('\nTheo Win: ', df_fp['theo_win'].sum())
print ('\nFreePlay Redeemed: ', df_fp['freeplay_redeemed'].sum())
print ('\nCash to Freeplay Stats \n')
print(df_fp['cash_to_fp'].describe(percentiles=[.1,.2,.3,.4,.5,.6,.7,.8,.9]))
print ('\nAmount Above Freeplay Stats \n')
print(df_fp['amt_above_fp'].describe(percentiles=[.1,.2,.3,.4,.5,.6,.7,.8,.9]))

以下是关键错误:

 File "<ipython-input-19-2aeece22e82c>", line 1, in <module>
    ga.group_analytics(df)

  File "GroupAnalysis_lc.py", line 174, in group_analytics
    df_fp = df(['freeplay_redeemed','cash_in', 'actual_win', 'theo_win'])

  File "C:\Users\crowl\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\frame.py", line 2056, in __getitem__
    return self._getitem_array(key)

  File "C:\Users\crowl\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\frame.py", line 2100, in _getitem_array
    indexer = self.loc._convert_to_indexer(key, axis=1)

  File "C:\Users\crowl\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\indexing.py", line 1231, in _convert_to_indexer
    raise KeyError('%s not in index' % objarr[mask])

KeyError: "['freeplay_redeemed' 'cash_in' 'actual_win' 'theo_win'] not in index"

0 个答案:

没有答案