title: How to create window and find mode for created window in pandas(Aggregate, window and finding mode)
body: <p>I have data frame(df) like this:</p>
  
  <pre><code>cpt     units
  36430   2
  36440   5
  36450   10
  36430   1
  36440   5
  36450   10
  36430   2
  36440   6
  36450   11
  
  </code></pre>
  
  <p>I need to add new column(called- mode) to above dataframe after aggregating on column(CPT), windowing and finding mode on column(units). I mean, I expect to see like this:</p>
  
  <pre><code>cpt units   mode
  36430   2   2
  36440   5   5
  36450   10  10
  36430   1   2
  36440   5   5
  36450   10  10
  36430   2   2
  36440   6   5
  36450   11  10
  </code></pre>
  
  <p>I have tried with following code</p>
  
  <pre><code>df['mode'] = df.groupby(['cpt'])['units'].transform(lambda x: x.mode)
  df['mode'] = df.groupby(['cpt'])['units'].transform(lambda x: x.mode())
  from scipy.stats import mode
  df['mode'] = df.groupby(['cpt'])['units'].transform(lambda x: mode(x))
  </code></pre>
  
  <p>No code is working as per my requirement though I am very near to my result. And I felt that on surfing internet, no direct command is there to solve my problem. Please help me out with code. THANKS IN ADVANCE.</p>