I have to loop through data including 'visit_counts' affiliated with a 'city'
I made an if statement in a for loop; it doesn't give any errors but it's also not printing what I want. Am I doing/understanding something wrong?
for v in visits_19:
if v == 'West Lafayette':
wl_sum = visits_19['raw_visit_counts'].sum()
print(wl_sum)
elif v == 'Lafayette':
l_sum = visits_19['raw_visit_counts'].sum()
print(l_sum)
elif v == 'Dayton':
d_sum = visits_19['raw_visit_counts'].sum()
print(d_sum)
elif v == 'Batle Ground':
bg_sum = visits_19['raw_visit_counts'].sum()
print(bg_sum)
Here is an example:
visit_19 = {'city': ['West Lafayette','Lafayette','Lafayette','West Lafayette','West Lafayette','Dayton'],
'visit_counts' : [100,10,151,20,2,5]}
df = pd.DataFrame(visit_19)
g = df['city']
for g in visit_19:
if g == 'West Lafayette':
wl_sum = df['visit_counts'].sum()
print(wl_sum)
elif g == 'Lafayette':
l_sum = df['visit_counts'].sum()
print(l_sum)
elif g == 'Dayton':
d_sum = df['visit_counts'].sum()
print(d_sum)
What I want to do: When the city == 'West Lafayette', calculate the sum of the visit that have West Lafayette as their city.
Aucun commentaire:
Enregistrer un commentaire