Is there a way to use bincount with a clause in python?
<p>I have hourly data about demand for bike rentals and weather. I would like to plot average demand for every hour, separately with good and bad weather.  </p>

<p>When I plotted average demand at a given hour (without taking into account weather) what I did was compute total demand for rentals at a given hour and then divide by total number of hours:</p>

<pre><code>hour_count = np.bincount(hour)
for i in range(number_of_observations):
    hour_sums[hour[i]] = hour_sums[hour[i]] + rentals[i]

av_rentals = [x/y for x,y in zip(hour_sums,hour_count)]
</code></pre>

<p>Now I would like to do the same, but separately for good weather and bad weather. The cumulative sum was easy, I just added an 'if' clause. I don't know what to do with counting hours of good and bad weather. I would prefer to avoid doing a big loop like with the sum... any function that does the same as bincount but with a clause? Something like:  </p>

<pre><code>good_weather_hour_count = np.bincount(hour, weather == 1 or weather == 2)
</code></pre>

<p>Any ideas?<br>
PS. Perhaps someone knows how to sum rentals for a given hour without a loop? I tried something with 2d histogram, but it didn't work.</p>

<pre><code>label_sums = np.histogram2d(hour, rentals, bins=24)[0]
</code></pre>