在groupby中,squeeze = True有什么作用? [英] What does squeeze = True do in groupby?

查看:432
本文介绍了在groupby中,squeeze = True有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现文档说如果可能的话,请减小返回类型的维数,否则返回一致的类型.

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': np.ones(4, dtype='float32'),
     'c': np.zeros(4, dtype='float32')})

df.groupby(df4.index,squeeze=True)['b'].sum()

无论有没有挤压,我都看不到任何变化.有人可以向我解释squeeze = True的真正目的,以及为什么默认情况下将其设置为false

I couldn't see any change with or without squeeze.Can someone explain me the real purpose of squeeze = True and why is it by default set to false

推荐答案

经过一番研究,如果可能的话,可以使用它来减小尺寸. @Jeff在github中显示的示例说明了为什么要使用完全挤压.在问题此处中对此进行了说明.

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参数来完成.很少有使用挤压的情况.

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.

这篇关于在groupby中,squeeze = True有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆