Issue
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.
Solution
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.
Answered By - yoskovia
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.