Python Forum
Loop Help: works with 300 variables but not 400
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop Help: works with 300 variables but not 400
#1
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 += 1
Here is the error I get:
Error:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-1-f2f7516a1eeb> in <module>() 81 summ2 = pd.concat([summ2, summ3]) 82 i += 1 ---> 83 summ2.columns=['FlagValue','Target0','Target1','Flag'] 84 summ2=summ2[summ2['FlagValue'] == 1 ] 85 if len(summ2)==0: #do this if else for when there are no more flags with hits //anaconda/lib/python2.7/site-packages/pandas/core/generic.pyc in __setattr__(self, name, value) 2981 try: 2982 object.__getattribute__(self, name) -> 2983 return object.__setattr__(self, name, value) 2984 except AttributeError: 2985 pass pandas/_libs/src/properties.pyx in pandas._libs.lib.AxisProperty.__set__ (pandas/_libs/lib.c:45103)() //anaconda/lib/python2.7/site-packages/pandas/core/generic.pyc in _set_axis(self, axis, labels) 469 470 def _set_axis(self, axis, labels): --> 471 self._data.set_axis(axis, labels) 472 self._clear_item_cache() 473 //anaconda/lib/python2.7/site-packages/pandas/core/internals.pyc in set_axis(self, axis, new_labels) 2834 raise ValueError('Length mismatch: Expected axis has %d elements, ' 2835 'new values have %d elements' % -> 2836 (old_len, new_len)) 2837 2838 self.axes[axis] = new_labels ValueError: Length mismatch: Expected axis has 3 elements, new values have 4 elements
Reply
#2
Quote:
 summ2.columns=['FlagValue','Target0','Target1','Flag']
Error:
ValueError: Length mismatch: Expected axis has 3 elements, new values have 4 elements
It sounds like they are expecting 3 elements where you have 4
Recommended Tutorials:
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020