0

I have a list of DataFrames i.e data = [df1,df2,df3.....dfn] . I am trying to iterate function maxloc through list of data and appending new values to new_max. It gives me following error TypeError: 'int' object is not iterable. How can I fix it?

def max(data): 
    data['loc_max'] = np.zeros(len(data))
    for i in range(1,len(data)-1):  
        if data['value'][i] >= data['value'][i-1] and data['value'][i] >= data['value'][i+1]:
            data['loc_max'][i] = 1
    return data 
def maxloc(data):
    loc_opt_ind = argrelextrema(df['values'].values, np.greater)
    loc_max = np.zeros(len(data))
    loc_max[loc_opt_ind] = 1
    data['loc_max'] = loc_max
    return data
new_max= []
for df in range(len(data)):
    max_values = maxloc(df).loc_max
    new_max.append(max_values)
Sam
  • 45
  • 1
  • 8
  • Could you include in your code section a simplified definition of `data = [df1,df2,df3.....dfn]` so that your error becomes reproducible? – Roberto Caboni Apr 07 '20 at 14:35
  • when you `for df in range(len(data)):` your df is just integers 1 2 3 ..., you need to change the loop on `for index, df in enumerate(data)` –  Apr 07 '20 at 14:36
  • Do you mean to do `for df in data:` so that each loop df be the next data frame in the list of dataframes called data? – Chris Doyle Apr 07 '20 at 14:37
  • @ChrisDoyle yes –  Apr 07 '20 at 14:37
  • @ChrisDoyle is it worked? Ff yes let me place it as an answer, If not, read comment of Roberto and give us some simple example, please –  Apr 07 '20 at 14:44
  • @Alexey yes it works thanks is it possible instead of new list ,new_max I append my max_values in same dataFrame with new column ? – Sam Apr 07 '20 at 14:49
  • @Sam yes, but it is question for another topic, you should see https://stackoverflow.com/questions/8486294/how-to-add-an-extra-column-to-a-numpy-array link or create new question. Please accept my answer below –  Apr 07 '20 at 15:07

1 Answers1

0

When you use:

for df in range(len(data)):
    # your loop

your df is just intgers, you should use this loop instea:

for df in data:
    # your loop