title: pandas : INDEXING a GroupBy object with a ARRAY OF COLUMN
body: <p>In Python for Data Analysis Book by Wes McKinne, it is mentioned that</p>
  <blockquote>
  <p>df.groupby('key1')[['data2']]</p>
  </blockquote>
  <p>is syntactically similar to</p>
  <blockquote>
  <p>df[['data2']].groupby(df['key1'])</p>
  </blockquote>
  <p>but the result produced by both statement are <strong>different</strong>. Moreover result of <em><strong>df.groupby('key1')[['data2']]</strong></em> is similar as <em><strong>df.groupby('key1')</strong></em></p>
  <pre><code>df = pd.DataFrame({'key1' : ['a', 'a', 'b', 'b', 'a'],
                 'key2' : ['one', 'two', 'one', 'two', 'one'],
                 'data1' : np.random.randn(5),
                 'data2' : np.random.randn(5)})
  df
  
  key1    key2    data1   data2
  0   a   one 1.103015    1.142491
  1   a   two 0.758543    -0.820083
  2   b   one -1.749065   0.445968
  3   b   two 0.337383    0.372625
  4   a   one -0.065351   -0.207071
  </code></pre>
  <hr />
  <pre><code>a = df.groupby('key1')[['data2']]
  
  for key, data in a:
    print(key)
    print(data)
  
  a
    key1 key2     data1     data2
  0    a  one  1.103015  1.142491
  1    a  two  0.758543 -0.820083
  4    a  one -0.065351 -0.207071
  b
    key1 key2     data1     data2
  2    b  one -1.749065  0.445968
  3    b  two  0.337383  0.372625
  </code></pre>
  <hr />
  <pre><code>c = df.groupby('key1')
  for key, data in c:
    print(key)
    print(data)
  
  a
    key1 key2     data1     data2
  0    a  one  1.103015  1.142491
  1    a  two  0.758543 -0.820083
  4    a  one -0.065351 -0.207071
  b
    key1 key2     data1     data2
  2    b  one -1.749065  0.445968
  3    b  two  0.337383  0.372625
  </code></pre>
  <hr />
  <pre><code>b = df[['data2']].groupby(df['key1'])
  for key, data in b:
    print(key)
    print(data)
  
  a
        data2
  0  1.142491
  1 -0.820083
  4 -0.207071
  b
        data2
  2  0.445968
  
  3  0.372625
  </code></pre>
  <hr />
  <p>Why the results stored in variable <strong>a</strong> and <strong>b</strong> are different when they should be similar as both are same but written differently mentioned by writer. Also why the results are similar for variable <strong>a</strong> and <strong>c</strong> as result of <strong>a</strong> should be different than <strong>c</strong> because of indexing . I don't understand the behaviour .</p>