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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With