I have some sample code that is goes as follows:
import numpy as np
import pandas as pd
x = range(1, 12)
arr1 = np.random.randint(x)
arr2 = np.array(x)
arr3 = np.random.randint(x)
arr4 = np.random.randint(x)
arr5 = np.random.randint(0, 2, 11)
dict_df = {
'arr1' : arr1,
'arr2' : arr2,
'arr3' : arr3,
'arr4' : arr4,
'arr5' : arr5
}
d = pd.DataFrame(dict_df)
num_count = 0
list_of_num = []
for i in d.index:
number = d['arr1'][i]
for num in d['arr5']:
if num == 1:
num_count = 1
number = number
list_of_num.append(number)
elif num == 0:
num_count = 0
print(list_of_num)
I am trying to build list, into which all of the ones in column arr5 are appended if they are preceeded by a -1. The output I am receiving from this is:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 1, 1, 1, 1, 1, 1, 5, 5, 5, 5, 5, 5, 2, 2, 2, 2, 2, 2, 8, 8, 8, 8, 8, 8]
The issue with the code is that I am misusing for loops, which is why the list has repeated itself so many times. How can I can I change the code so that the code does not repeat itself?