title: Python3 How to implement sliding window counts on Pandas Dataframe
body: <p>I want to create a plot from a large Pandas dataframe. The data is in the following format</p>
  
  <pre><code>Type     Number    ...unimportant additional columns
  
  Foo       13  ...
  Foo       25  ...
  Foo       56  ...
  Foo       56  ...
  Bar       10  ...
  Bar       10  ...
  Bar       11  ...
  Bar       23  ...
  </code></pre>
  
  <p>I need to count the number of elements from column 'Number' in a sliding window from x to x+i to determine the number of values falling in each sliding window bucket.</p>
  
  <p>For example, if the window size is i=10, starting at x=0, and incrementing x by 1 each step, the sliding window bucket for 'Foo' a correct result for the above example would be:</p>
  
  <pre><code>      Foo    Bar
  
    0    0      2    #(0-10)
    1    0      3    #(1-11)
    2    0      3    #(2-12)
    3    1      3    #(3-13)
    4    1      3    #(4-14)
     .
     .
     .
    20   1      1    #(13-23)
    21   0      1    #(14-24)
    22   1      1    #(15-25)
     . 
     .
     .
  </code></pre>
  
  <p>The answer would have df.max().max - [Window Length] rows, and len(df.columns) columns.</p>
  
  <p>Toy code to generate a similar dataframe might be the following:</p>
  
  <pre><code>import pandas as pd
  import numpy as np
  
  str_arr = ['Foo','Bar','Python','PleaseHelp']
  data1 = np.matrix(np.random.choice(str_arr, 100, p=[0.5, 0.1, 0.1, 0.3])).T
  data2 = np.random.randint(100, size=(100,1))
  merge = np.concatenate((data1,data2), axis=1)
  
  df = pd.DataFrame(merge, index=range(100), columns=['Type','Number'])
  df.sort_values(['Type','Number'], ascending=[True,True], inplace=True)
  df = df.reset_index(drop=True)
  </code></pre>
  
  <p>How can I generate such a list efficiently? </p>
  
  <p>Edit Note: Thanks to FLab who answered my question earlier before I clarified my question.</p>