Skip to content Skip to sidebar Skip to footer

What Does Squeeze = True Do In Groupby?

I found doc says reduce the dimensionality of the return type if possible,otherwise return a consistent type. df = pd.DataFrame( {'a': np.ones(4, dtype='float32'), 'b':

Solution 1:

After a bit of research it is used to reduce the dimension if possible. An example showed by @Jeff in github shows why exactly squeeze is used. Its stated in the issue here.

df1 = pd.DataFrame(dict(A = range(4), B = 0))

def func(dataf):
    return pd.Series({ dataf.name : 1})


result1 = df1.groupby("B",squeeze=False).apply(func)
   0
B   
0  1
type(result1)
pandas.core.frame.DataFrame

result2 = df1.groupby("B",squeeze=True).apply(func)

B   
0  0    1
Name: 0, dtype: int64

type(result2)
pandas.core.series.Series

Squeeze will try to reduce the dimension if its possible to reduce. As you can see above dataframe can be reduced to series so it was done via squeeze parameter. There are very less cases of use of squeeze.


Post a Comment for "What Does Squeeze = True Do In Groupby?"