Q:

Appending pandas dataframe and adding its own values (python)

my dataframe looks like:
hours  |  cumulative value  |  value to add
  1              1                  1
  2              2                  1
  3              3                  3
  4              6                  1

What I want is, to append this dataframe like e.g.: 2-times (should be up to 50-times) and add its own values. It should look like:
hours  |  cumulative value |  value to add
  1              1                 1
  2              2                 1
  3              3                 3
  4              6                 1
  #The first time 
  5              7                 1
  6              8                 1
  7              9                 3
  8              12                1
  #The second time
  9              13                1
  10             14                1
  11             15                3
  12             18                1

And so on..
I am strugglin to find an optimal solution for that. Has anyone a good idea?

A:

Here's a little function that can achieve this for you on a row by row basis (keeps appending new rows to the data frame)
df = pd.DataFrame({'hours':[1,2,3,4],
                   'cumulative value':[1,2,3,6], 
                   'Value to add': [1,1,3,1]})

def add_row(dataframe, numtimes): #adds new row 'numtimes' many times
    for i in range(numtimes):

        # Get values to put in new row

        new_hour = dataframe['hours'].iloc[-1]+1
        new_cumulative_value = dataframe['cumulative value'].iloc[-1]+dataframe['Value to add'].iloc[-1]
        if new_hour % 3 == 0:
            new_val_to_add = 3
        else:
            new_val_to_add = 1

        #Create new row and add to DataFrame

        new_row = pd.DataFrame({'hours':[new_hour],
                                'cumulative value':[new_cumulative_value],
                                'Value to add': [new_val_to_add]},
                                index=[new_hour-1])

        dataframe = dataframe.append(new_row)

    return dataframe

df =add_row(df, 50)

