title: How to calculate a weighted average in Python for each unique value in two columns?
body: <p>The picture below shows a few lines of printed lists I have in Python. I would like to get: a list of unique values of boroughs, a corresponding list of unique values of years, and a list of weighted averages of &quot;averages&quot; with &quot;nobs&quot; as weights but for each borough and each year (the variable &quot;type&quot; indicates if there was just one, two or three types in a specific year in a borough).</p>
  <p>I know how to get a weighted average using the entire lists:</p>
  <pre><code>weighted_avg = np.average(average, weights=nobs)
  </code></pre>
  <p>But I don't know how to calculate one for each unique borough-year.</p>
  <p><a href="https://i.stack.imgur.com/5d0zk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5d0zk.png" alt="enter image description here" /></a></p>
  <p>I'm new to Python, please help if you know how to do it.</p>