How to access DataFrame column created with DataFrame.groupby
<p>After creating a DataFrame with a column 'a' having the duplicated cell values:</p>

<pre><code>import pandas as pd
df = pd.DataFrame({'a': [1,2,2,3,3,3,3],
                   'b':[1,2,3,4,5,6,7],
                   'c':[8,9,10,11,12,13,14]})
</code></pre>

<p><a href="http://i.stack.imgur.com/OlBbV.png" rel="nofollow"><img src="http://i.stack.imgur.com/OlBbV.png" alt="enter image description here"></a></p>

<p>I proceed by creating a copy of it where I consolidate the duplicated cells in column 'a' while summing the values of other cells. I am using <code>groupby</code> method to achieve this, like so:</p>

<pre><code>groupDf = df.groupby('a', axis=0).sum()
</code></pre>

<p><a href="http://i.stack.imgur.com/Pullb.png" rel="nofollow"><img src="http://i.stack.imgur.com/Pullb.png" alt="enter image description here"></a></p>

<p>After the consolidated version of the DataFrame is created I want to access its columns ['a']:</p>

<pre><code>print(groupDf['a])
</code></pre>

<p>and I am getting the <code>KeyError</code> notifying me that ['a'] column does not exist.
Printing the column names with <code>groupDf.columns</code> returns only the column 'b' and the column 'c': <code>Index(['b', 'c'], dtype='object')</code></p>

<p>How should I access the column 'a? </p>

<p>After the <code>groupby</code>, the grouped column turns into an index, you can access it either by call <code>.index</code> or <code>reset_index</code> and then access it as a normal column, i.e the following two methods:</p>

<pre><code>df.groupby('a', axis=0).sum().reset_index()

#   a   b   c
# 0 1   1   8
# 1 2   5   19
# 2 3   22  50

df.groupby('a', axis=0).sum().index
# Int64Index([1, 2, 3], dtype='int64')
</code></pre>

python pandas