Single Positional Index Out-of-Bounds - Python

Viewed 39

I am working on dictionary of dataframes--five in total, and I am trying to grab the first row of each dataframe. My code appears to work for some of the dataframes, but not others. The sample dataframes are below:

a0 = {'dataset': {'0.05, 1.0, 175': 'CS:1', '0.05, 1.0, 150': 'CS:1'},
 'mean_score': {'0.05, 1.0, 175': -0.2820450154520415,
  '0.05, 1.0, 150': -0.28204501545204186},
 'rank_score': {'0.05, 1.0, 175': 1, '0.05, 1.0, 150': 2},
 'std_score': {'0.05, 1.0, 175': 0.11499605607995111,
  '0.05, 1.0, 150': 0.11499605607995127},
 'contamination': {'0.05, 1.0, 175': 0.05, '0.05, 1.0, 150': 0.05},
 'max_samples': {'0.05, 1.0, 175': 1.0, '0.05, 1.0, 150': 1.0},
 'n_estimators': {'0.05, 1.0, 175': 175, '0.05, 1.0, 150': 150}}

a1 = {'dataset': {'0.05, 1.0, 200': 'CK:-1', '0.05, 1.0, 175': 'CK:-1'},
 'mean_score': {'0.05, 1.0, 200': -0.20618057780261195,
  '0.05, 1.0, 175': -0.20618057780261267},
 'rank_score': {'0.05, 1.0, 200': 1, '0.05, 1.0, 175': 2},
 'std_score': {'0.05, 1.0, 200': 0.13109535628226052,
  '0.05, 1.0, 175': 0.13109535628226013},
 'contamination': {'0.05, 1.0, 200': 0.05, '0.05, 1.0, 175': 0.05},
 'max_samples': {'0.05, 1.0, 200': 1.0, '0.05, 1.0, 175': 1.0},
 'n_estimators': {'0.05, 1.0, 200': 200, '0.05, 1.0, 175': 175}}

a2 = {'dataset': {'0.05, 0.7, 125': 'PH:1',
  '0.05, 0.7749999999999999, 200': 'PH:1'},
 'mean_score': {'0.05, 0.7, 125': -0.22096885360666768,
  '0.05, 0.7749999999999999, 200': -0.22416479620828117},
 'rank_score': {'0.05, 0.7, 125': 1, '0.05, 0.7749999999999999, 200': 2},
 'std_score': {'0.05, 0.7, 125': 0.05228492731122392,
  '0.05, 0.7749999999999999, 200': 0.061897704957581456},
 'contamination': {'0.05, 0.7, 125': 0.05,
  '0.05, 0.7749999999999999, 200': 0.05},
 'max_samples': {'0.05, 0.7, 125': 0.7,
  '0.05, 0.7749999999999999, 200': 0.7749999999999999},
 'n_estimators': {'0.05, 0.7, 125': 125, '0.05, 0.7749999999999999, 200': 200}}

a3 = {'dataset': {'0.05, 0.85, 125': 'PRT:-1',
  '0.05, 0.85, 100': 'PRT:-1'},
 'mean_score': {'0.05, 0.85, 125': -0.12896828405478034,
  '0.05, 0.85, 100': -0.13141635454748085},
 'rank_score': {'0.05, 0.85, 125': 1, '0.05, 0.85, 100': 2},
 'std_score': {'0.05, 0.85, 125': 0.016228240324984843,
  '0.05, 0.85, 100': 0.013178168219693726},
 'contamination': {'0.05, 0.85, 125': 0.05, '0.05, 0.85, 100': 0.05},
 'max_samples': {'0.05, 0.85, 125': 0.85, '0.05, 0.85, 100': 0.85},
 'n_estimators': {'0.05, 0.85, 125': 125, '0.05, 0.85, 100': 100}}

a4 = {'dataset': {'0.05, 1.0, 200': 'PRT:1',
  '0.05, 1.0, 175': 'PRT:1'},
 'mean_score': {'0.05, 1.0, 200': -0.1694053747115974,
  '0.05, 1.0, 175': -0.1694053747115976},
 'rank_score': {'0.05, 1.0, 200': 1, '0.05, 1.0, 175': 2},
 'std_score': {'0.05, 1.0, 200': 0.006550547930259526,
  '0.05, 1.0, 175': 0.006550547930259387},
 'contamination': {'0.05, 1.0, 200': 0.05, '0.05, 1.0, 175': 0.05},
 'max_samples': {'0.05, 1.0, 200': 1.0, '0.05, 1.0, 175': 1.0},
 'n_estimators': {'0.05, 1.0, 200': 200, '0.05, 1.0, 175': 175}}

The code to select row values based on the column is also shown below. The codes do work as expected for dataframes a0 through a2, but then with dataframes a3 - a4, I get the index positional error message.

subsets = ['CS:1', 'CK:-1', 'PH:1', 'PRT:-1', 'PRT:1']  # rows to select


#the code to retrieve the first rows for a0-a2 work as expected.
(a0.loc[a0['dataset'].isin([subsets[0]])].iloc[0][['contamination', 'max_samples', 'n_estimators']])

(a1.loc[a1['dataset'].isin([subsets[1]])].iloc[0][['contamination', 'max_samples', 'n_estimators']])

(a2.loc[a2['dataset'].isin([subsets[2]])].iloc[0][['contamination', 'max_samples', 'n_estimators']])

# the codes to retrieve the first frow for a3 - a4 give the single positional indexer is out-of-bounds error.
(a3.loc[a3['dataset'].isin([subsets[3]])].iloc[0][['contamination', 'max_samples', 'n_estimators']])

(a4.loc[a4['dataset'].isin([subsets[4]])].iloc[0][['contamination', 'max_samples', 'n_estimators']])  

Not sure why, since I am not using an index that is out of bounds.

1 Answers

Using your samples, I cannot replicate the error. Anyhow, the "IndexError: single positional indexer is out-of-bounds" in your code can only come from .iloc[0] which in turn can only produce this error, if there are no rows at all in the DataFrame it is used on. So the most likely cause of your error is the DataFrame not containing the string from the subsets list in the "dataset" column.

Related