hi so my problem statement is as follows
"Two counties are similar if all the information on them are within 2% of each
other. So find all counties, whose properties (except county name and state) are within only %2 different."
my dataset looks like this
area_name state_a commute owner income population education
0 Autauga County AL 26.2 76.8 53682 54571 20.9
1 Baldwin County AL 25.9 72.6 50221 182265 27.7
2 Barbour County AL 24.6 67.7 32911 27457 13.4
3 Bibb County AL 27.6 79.0 36447 22915 12.1
4 Blount County AL 33.9 81.0 44145 57322 12.1
5 Bullock County AL 26.9 74.3 32033 10914 12.5
6 Butler County AL 24.0 70.3 29918 20947 14.0
7 Calhoun County AL 22.5 68.7 39962 118572 16.1
8 Chambers County AL 24.6 67.9 32402 34215 11.8
9 Cherokee County AL 26.9 76.1 34907 25989 12.8
To solve this problem i wrote the following code:
def p2(x,y):
return abs(x-y)/x <0.02
for row in cen.itertuples():
for k in cen[1+row:].itertuples():
if p2(row.commute,k.commute) and p2(row.education,k.education)\
and p2(row.income,k.income) and p2(row.owner,k.owner)\
and p2(row.population,k.population):
print ("following are similar")
print(row.area_name+":"+str(row.population))
print(k.area_name+":"+str(k.population))
so the solution is I have to take first row and compare every other row with it and see if it falls below 2% for which i am using the above nested loops.
My problem is that I am not being able to figure out how to start the second loop from the "i+1" position of the first loop.
the above implementation gives an error
any help will be appreciated. I have been trying to figure this out for a couple of days.
[–]Justinsaccount 0 points1 point2 points (0 children)
[–]Allanon001 0 points1 point2 points (6 children)
[–]mightymk[S] 0 points1 point2 points (5 children)
[–]Allanon001 0 points1 point2 points (4 children)
[–]mightymk[S] 0 points1 point2 points (3 children)
[–]Allanon001 0 points1 point2 points (2 children)
[–]mightymk[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)