【问题标题】:ValueError: len(index) != len(labels) for groupby pandasValueError: len(index) != len(labels) for groupby pandas
【发布时间】:2020-11-29 08:55:15
【问题描述】:

我正在尝试在 python 中使用简单的 groupby 函数,但遇到了问题。我在 python 中有多个相同的日期,并试图取一个平均值。我的数据如下所示:

1    All dwellings  date
222        63810.0  1992
223        65183.0  1992
224        60818.0  1992
225        62397.0  1993
226        63218.0  1993

当我尝试使用 groupby 时,我得到了这个错误:

uk_house.groupby(by='date', axis=1).mean()

ValueError: len(index) != len(labels)

我已经查看了这个错误,它似乎是在尺寸发生变化时发生的,但这肯定是 groupby 的全部意义吗?

谁能帮我理解这一点。谢谢

【问题讨论】:

    标签: python pandas pandas-groupby


    【解决方案1】:

    如果您尝试对 dweelings 列进行平均,您可能必须使用以下代码

    uk_house.groupby(['date'])[['dweelings']].mean()
    

    【讨论】:

      【解决方案2】:

      不要加axis=1,当你通过axis=1时,pandas会做column groupby而不是row

      uk_house.groupby('date').mean()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-11
        • 2019-12-09
        • 1970-01-01
        相关资源
        最近更新 更多