I have been trying to code this for a while. here is a sample dataframe:
capacity = 500
s = pd.Series(['School 1','School 2', 'School 3','School 4', 'School 5'])
p = pd.Series(['132', '458', '333', '300', '258'])
d = pd.Series(['1', '2', '3', '4', '5'])
df = pd.DataFrame(np.c_[s,p,d],columns = ['School Name','Population', 'Distance'])
What I want to do is to make loop where loop will continually subtract the 'Population' from the 'capacity' as long as it does not exceed the capacity. It would need to check the 'Distance' for the order.
example: Since 'School 1' is the nearest it subtracts 132 from 500 which is 368. But since 'School 2' is the next nearest but the population exceeds 368 (458>368), it would stop here, it would no longer continue to check the next nearest School which is 'School 3'.
After this it should then assign the school name in to another column
end result would be:
s = pd.Series(['School 1','School 2', 'School 3','School 4', 'School 5'])
p = pd.Series(['132', '458', '333', '300', '258'])
d = pd.Series(['1', '2', '3', '4', '5'])
sn = pd.Series(['School 1', 0, 0 ,0 ,0])
df2 = pd.DataFrame(np.c_[s,p,d,sn],columns = ['School Name','Population', 'Distance','Included'])
Been trying to work on this since yesterday, still have no clue how to do it except manually. Still a beginner python user.
Thanks for the help!
Python loop, how to loop this?" isn't a so good title tho..(458 > 368)in your example. There is a typo (368is written as468).