2

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

Jonathan Budez
  • 226
  • 1
  • 3
  • 12
  • What is `newdf`? What shape is it? What is the index on it vs on `df`? – Nathan Mar 22 '19 at 16:35
  • Nathan, I modified the question to clarify it, I perform other operation to the df, however, for the purpose of the question I do not think it is necessary to provide those details. The main goal is explained above. – Jonathan Budez Mar 22 '19 at 16:40
  • I get no errors with your code if you make the edits I just suggested (make `df` a `pd.DataFrame` and change `d.drop` to `df.drop`). – Nathan Mar 22 '19 at 16:48
  • Do you still have an issue/question or does this work now? – Nathan Mar 23 '19 at 15:05
  • @Nathan I have edited the question, the error rise because of the dimensions of the two dataframes. However, the dimension of the newdf on the question needs to be 1000 rows and 5 columns, that is the reason why I get that error, but I have not been able to solve it. – Jonathan Budez Mar 24 '19 at 13:22

1 Answers1

2

The issue occurs not because of anything that has to do with df, but because when i = 5, newdf[:, i] refers to the sixth column of a NumPy array containing only five columns. If, instead, you initialize newdf through newdf = np.zeros([1000, 10]), or loop only over range(5), then your code runs without errors.

fuglede
  • 17,388
  • 2
  • 54
  • 99