Reputation: 336
I have a dictionary of dataframes. I defined the dict as range_, such that when I need a certain dataframe, I can call it range_[i].
For every data frame in the dictionary, I want to add an extra column. So what I did is the following:
for i in range_selected:
for index in range_[i].index:
range_[i].loc[index,'label(1 = fault)'] = ''
Now, I want to add a value in every row of this new column. It can be 0 or 1. My condition is that if the value of 'pdis1' at the first row of every dataframe - the value of the 9-10th row is smaller than zero, then append 1 for every row of the new column, else 0. I tried with this:
for r in range_:
for index in range_[r].index:
if range_[r]['pdis1'].iloc[0] - range_[r]['pdis1'].iloc[10]:
range_[r].loc[index,'label(1 = fault)'] = '1'
but I get 'IndexError: single positional indexer is out-of-bounds'
Can anyone help me out? Thank you
Upvotes: 0
Views: 284
Reputation: 46
That happens when you try to access an index that does not exist in the DF.
If you are sure that all the data frames have at least 10 rows, then this should work. If you still get this error, it means that one of the DataFrames has less than 10 rows. Thus your rule for determining the faultiness is invalid.
for df in range_.values():
if df['pdis1'].iloc[0] - df['pdis1'].iloc[10]:
val = '1'
else:
val = '0'
df['label(1 = fault)'] = val
I changed the iteration to look a bit more pythonic : )
Upvotes: 1