Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groupby apply makes an unwanted transpose when there is only one group

Pandas has an inconsistent behavior when doing a groupby-apply with one group :

(
    pd.DataFrame({'c1': [0, 0, 0],
                  'c2': [1, 2, 3]})
    .groupby('c1')
    .apply(lambda df: df['c2']).shape
)

is equal to (1, 3)

while

(
    pd.DataFrame({'c1': [0, 0, 1],
                  'c2': [1, 2, 3]})
    .groupby('c1')
    .apply(lambda df: df['c2']).shape
)

is equal to (3, ).

When there only one unique value in the groupby variable, the resulting Serie is transposed from what I expect.

I need a consistent behavior : the number of rows must be kept at 3 no matter the number of groups.

like image 797
Rodolphe LAMPE Avatar asked Sep 16 '25 23:09

Rodolphe LAMPE


1 Answers

You can specify squeeze=True in the .groupby(...) when you are grouping by a column that only has one value, like this:

(
    pd.DataFrame({'c1': [0, 0, 0], 
                  'c2': [1, 2, 3]})
    .groupby('c1', squeeze=True)
    .apply(lambda df: df['c2']).shape
)

See the documentation here.

like image 123
yoskovia Avatar answered Sep 19 '25 14:09

yoskovia