user17144
user17144

Reputation: 438

How to access size column in groupby

Consider the following code.

d=pd.DataFrame([[1,'a'],[1,'b'],[2,'c'],[2,'a'],[3,'c'],[4,'a'],[4,'c']],columns=['A','B'])
k=d.groupby(d.A).size().to_frame('size')

It returns

   size
A
1     2
2     2
3     1
4     2

Also,

k.shape

(4,1)

I want to be able to access the size column and the index column but the data frame is only 1 column wide. How do I access both columns?

Upvotes: 0

Views: 610

Answers (1)

Gorlomi
Gorlomi

Reputation: 515

Actually, 'size' column is the only column you can access, the one on the left is simply an index.

If you want to have that index as a column as well you could do the following:

d=pd.DataFrame([[1,'a'],[1,'b'],[2,'c'],[2,'a'],[3,'c'],[4,'a'],[4,'c']],columns=['A','B'])
k=d.groupby(d.A).size().to_frame('size')

k.reset_index(level=0, inplace=True)

Now you would be able to access both columns and have a new index.

    A   size
0   1   2
1   2   2
2   3   1
3   4   2

k.shape

(4,2)

i.e.

k['A'][0]
1

or

k['size'][0]
2

Hope this helped!

Upvotes: 1

Related Questions