代码之家  ›  专栏  ›  技术社区  ›  user288609

grouby对象的形状及其转换的dataframe对象

  •  0
  • user288609  · 技术社区  · 6 年前

    B 由groupby操作返回。我打印了它的长度,即6。然后我将这个groupby对象转换为dataframe类型,然后我打印了这个转换后的dataframe对象的形状,形状与原始groupby对象不匹配。我很困惑。请查看相关代码和结果,谢谢。

    print('type of B', type(B))
    print('len(B) ',len(B))
    b=B.apply(pd.DataFrame)
    print('type of b', type(b))
    print('b shape' , b.shape)
    print('len(b) ',len(b))
    
    type of B <class 'pandas.core.groupby.generic.DataFrameGroupBy'>
    len(B)  6
    type of b <class 'pandas.core.frame.DataFrame'>
    b shape (84, 99)
    len(b)  84
    
    0 回复  |  直到 6 年前
        1
  •  0
  •   BENY    6 年前

    第一个是 groupby 对象,它将等于唯一的 申请时请输入密码

    df=pd.DataFrame({'a':[1,1,2,2]})
    len(df.groupby('a'))
    Out[49]: 2
    len(df.groupby('a'))==df.a.nunique()
    Out[50]: True
    

    我们之后 apply(pd.DataFrame) ,groupby对象更改回dataframe,因此它将等于相同长度的b