Reputation: 107
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?
Upvotes: 0
Views: 211
Reputation: 8631
You need to repeat value to add
and then compute other columns. It is best if you make a series
separately and then simply create a new dataframe.
s = pd.Series((df['value to add'].tolist()) *50)
data = pd.DataFrame({
'hour': range(1, len(s)+1),
'cumulative value' : s.shift().fillna(s[0]).cumsum(),
'value to add': s
})
data.head(10)
Output:
hour cumulative value value to add
0 1 1 1.0
1 2 2 2.0
2 3 5 3.0
3 4 6 6.0
4 5 7 7.0
5 6 8 8.0
6 7 11 9.0
7 8 12 12.0
8 9 13 13.0
9 10 14 14.0
Upvotes: 1
Reputation: 444
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)
Upvotes: 1
Reputation: 1559
from functools import reduce
def repeat(df,number):
series1 = np.tile(np.array(df['value to add']),number)
series2 = reduce((lambda x,y: np.append(x,y+x[-1])),[np.array(df['cumulative value'] for i in range(number)])
series3 = reduce((lambda x,y: np.append(x,y+x[-1])),[np.array(df['hours'] for i in range(number)])
return(pd.DataFrame({'hours':series3,'cumulative value':series2,'value to add':series1})
Running this function with number
=3 gives
hour cumulative value value to add
0 1 1 1
1 2 2 1
2 3 5 3
3 4 6 1
4 5 7 1
5 6 8 1
6 7 11 3
7 8 12 1
8 9 13 1
9 10 14 1
Upvotes: 0
Reputation: 2945
new_df = pd.concat([df] * 50, ignore_index=True)
new_df["hours"] = np.arange(df.shape[0])
new_df["cumulative_value"] = df["value_to_add"].cumsum()
Upvotes: 0