user1107049 user1107049 - 1 month ago 5
Python Question

How to access DataFrame column created with DataFrame.groupby

After creating a DataFrame with a column 'a' having the duplicated cell values:

import pandas as pd
df = pd.DataFrame({'a': [1,2,2,3,3,3,3],

enter image description here

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
method to achieve this, like so:

groupDf = df.groupby('a', axis=0).sum()

enter image description here

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


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

How should I access the column 'a?


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

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')