# My loop only saves the result of last iteration

2476 views
6

``````import numpy as np
import pandas as pd
``````

This is my data:

``````ts = pd.DataFrame([0,1,2,3,4,5,6,7,8,9,10,11,12])
ts.columns = ["TS"]
start_df = pd.Series([1,3,6])
end_df = pd.Series([2,7,10])
``````

I have created the following function to clean up my loop, and a for loop to iterate over each element in ts and save according to the output of `check_if`.

``````def check_if(start, ts, end):
if start <= ts <= end:
return 1
else:
return 0

ts["Flagg"] = np.nan
for ix, hour in enumerate (ts["TS"]):
for jx, end in enumerate(end_df):
ts["Flagg"][ix] = check_if(start_df[jx], hour, end_df[jx])
``````

The problem is that my resulting `ts["Flagg"]` only saves the result of the last iteration, `start_df == 6` and `end_df == 10`. Is my logic in the loop completely of?

Edit:
Expected output

``````[0,1,1,1,1,2,2,1,1,1,0,0]
``````

in column `ts["Flagg"]`.

What is expected output?

7

Use `between` with list comprehension for list of boolean mask and then `sum` it for count values, thanks @RafaelC for improvement:

``````ts['new'] = np.sum([ts['TS'].between(x, y) for x, y in zip(start_df, end_df)], axis=0)
print (ts)
TS  new
0    0    0
1    1    1
2    2    1
3    3    1
4    4    1
5    5    1
6    6    2
7    7    2
8    8    1
9    9    1
10  10    1
11  11    0
12  12    0
``````

posted this