Loop Help: works with 300 variables but not 400 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Loop Help: works with 300 variables but not 400 (/thread-5753.html) |
Loop Help: works with 300 variables but not 400 - bpozza - Oct-19-2017 Any help will be greatly appreciated. Please let me know if you need more details to resolve and I can provide. Thank you in advance for your help. ##ERROR IN THIS LOOP SECTION WHEN len(cols2) = 400 BUT NOT WHEN = 300 ## cols2 is a list that starts with FLAG001 and goes in numerical order to FLAG300 ## the loop works when cols2 goes to FLAG300 (or less) ## the loop does not work when cols2 goes to FLAG400 b = 1 while b < len(cols2): summ2= pd.DataFrame() i = 0 while i < len(cols2): z=cols2[i] summ3=datafile4.groupby([z, 'TARGET']).size().unstack() summ3['Variable']=z #create column to know which variable summ3.index.names = ['Category'] #change index column name to Category summ3.reset_index(level=None, inplace=True) #change index from index to column summ3['Key'] = summ3.Variable + summ3.Category.map(str) #create new column to be index summ3.set_index('Key', inplace=True) # set column as index summ2 = pd.concat([summ2, summ3]) i += 1 summ2.columns=['FlagValue','Target0','Target1','Flag'] summ2=summ2[summ2['FlagValue'] == 1 ] if len(summ2)==0: #do this if else for when there are no more flags with hits summ2.loc[0] = [1.0, 0.0, 0.0,'FLAG999'] summ2['Rank']=b+1 incsum=pd.concat([incsum, summ2.iloc[[0]]]) else: summ2.sort_values(by=['Target1', 'Target0'], ascending=[False, True], inplace=True) summ2['Rank']=b+1 incsum=pd.concat([incsum, summ2.iloc[[0]]]) y=summ2.iloc[0]['Flag'] datafile4=datafile4[datafile4[y] == 0 ] b += 1Here is the error I get:
RE: Loop Help: works with 300 variables but not 400 - metulburr - Oct-22-2017 Quote:It sounds like they are expecting 3 elements where you have 4summ2.columns=['FlagValue','Target0','Target1','Flag'] |