I am doing some modifications to a dataframe with a for loop. I am adding a new column every cycle of the for loop, however, I also drop this column at the end of the cycle. I would like to know if it is possible to store the values of this column per cycle, and create a new dataframe that is made of each of these columns that were generated per cycle. I am using the following code:
import numpy as np
import pandas as pd
newdf = np.zeros([1000,5])
df = pd.DataFrame(np.random.choice([0.0, 0.05], size=(1000,1000)))
for i in range(0, 10):
df['sum']= df.iloc[:, -1000:].sum(axis=1)
newdf[:,i] = df['sum']
df = df.drop('sum', 1)
However, I get the following error:
index 5 is out of bounds for axis 1 with size 5
Thanks