I have a dataframe I want to iterate through using a function, but can't seem to use itertools correctly. The below nested for loop works fine!
columns = len(df_test.columns)
rows = len(df_test.index)
list = []
list2 = []
def inRange(df, min, max):
for i in range(rows):
x = 0
for j in range(columns):
if df_test.iloc[i, j] >= min and df_test.iloc[i, j] <= max:
x = x + 1
else:
x = x + 0
list2.append((i, j))
if x == columns:
list.insert(i, "True")
# print("True")
else:
# print("False")
list.insert(i, "False")
When I use itertools, it does not. It gives only 1 False value added into the list, when it should be [False, True, True, False].
def inRange(df, min, max):
x = 0
# i = index, j = columns
for i, j in itertools.product(range(rows), range(columns)):
if df_test.iloc[i, j] >= min and df_test.iloc[i, j] <= max:
x = x + 1
else:
x = x + 0
list2.append((i, j))
if x == columns:
list.insert(i, "True")
# print("True")
else:
# print("False")
list.insert(i, "False")
Expected: list = [False, True, True, False]
list2 = [(0, 0), (0, 1), (0, 3), (3, 2), (3, 3)]
list 2 works fine and gives the correct result.