title: scikit-learn impute mean of feature within groups of nominal value in another feature
body: <p>I want to impute the mean of a feature but only calculate the mean based off other examples that have the same category/nominal value in another column and I was wondering if this was possible using scikit-learn's Imputer class? It would just make it easier to add into a pipeline that way.</p>
  
  <p><em>For example:</em></p>
  
  <p>Using the Titanic dataset from kaggle: <a href="https://www.kaggle.com/c/titanic/data" rel="noreferrer">source</a></p>
  
  <p>How would I go about imputing the mean <code>fare</code> per <code>pclass</code>. The thinking behind it being that people in different classes would have large differences in cost between tickets.</p>
  
  <p><em>Update:</em> After discussion with some people, the phrase I should have used was "imputing the mean <em>within class</em>".</p>
  
  <p>I've looked into <a href="https://stackoverflow.com/users/3374996/vivek-kumar">Vivek</a>'s comment below and will construct a generic pipeline function when I get time to do what I want :) I have a good idea of how to do it and will post as an answer when it's finished.</p>