import math
avg_cost = []
for [e,l] in lis:
length = ZomatoData[(ZomatoData["establishment"]==e) & (ZomatoData["locality"]==l)].shape[0]
avg_cost.append(math.ceil(ZomatoData[(ZomatoData["establishment"]==e) & (ZomatoData["locality"]==l) & (ZomatoData["average_cost_for_two"]!=0)]["average_cost_for_two"].mean()*length/(length-1)))
avg_cost
ZeroDivisionError Traceback (most recent call last)
<ipython-input-189-419eb31f025c> in <module>()
3 for [e,l] in lis:
4 length = ZomatoData[(ZomatoData["establishment"]==e) & (ZomatoData["locality"]==l)].shape[0]
----> 5 avg_cost.append(math.ceil(ZomatoData[(ZomatoData["establishment"]==e) & (ZomatoData["locality"]==l) & (ZomatoData["average_cost_for_two"]!=0)]["average_cost_for_two"].mean()*length/(length-1)))
6 avg_cost
ZeroDivisionError: float division by zero
Can anybody help me with this?
Could you not condition your append statement as such:
import math
avg_cost = []
for [e,l] in lis:
length = ZomatoData[(ZomatoData["establishment"]==e) & (ZomatoData["locality"]==l)].shape[0]
if length != 1:
avg_cost.append(math.ceil(ZomatoData[(ZomatoData["establishment"]==e) & (ZomatoData["locality"]==l) & (ZomatoData["average_cost_for_two"]!=0)]["average_cost_for_two"].mean()*length/(length-1)))
avg_cost
Since the "divide by zero" scenario appears to happen if length is equal to "1".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.