【问题标题】:Pandas: ValueError - operands could not be broadcast together with shapesPandas:ValueError - 操作数无法与形状一起广播
【发布时间】:2015-05-18 22:15:36
【问题描述】:

在大型数据帧上执行add()combine_first() 之类的操作时出现以下运行时错误:

ValueError: operands could not be broadcast together with shapes (680,) (10411,)

广播错误似乎经常发生使用 Numpy(矩阵尺寸不匹配),但我不明白为什么它会影响我的多索引数据帧/系列。每个 concat 元素都会产生一个运行时错误:

我的代码:

# I want to merge two dataframes data1 and data2
# add up the 'requests' column
# merge 'begin' column choosing data1-entries first on collision
# merge 'end' column choosing data2-entries first on collision

pd.concat([\
    data1["begin"].combine_first(data2["begin"]),\
    data2["end"].combine_first(data1["end"]),\
    data1["requests"].add(data2["requests"], fill_value=0)\
    ], axis=1)

我的数据:

# data1
                           requests               begin                 end
IP              sessionID
*1.*16.*01.5*   20                9 2011-12-16 13:06:23 2011-12-16 16:50:57
                21                3 2011-12-17 11:46:26 2011-12-17 11:46:29
                22               15 2011-12-19 10:10:14 2011-12-19 16:10:47
                23                9 2011-12-20 09:11:23 2011-12-20 13:01:12
                24                9 2011-12-21 00:15:22 2011-12-21 02:50:22
...
6*.8*.20*.14*   6283              1 2011-12-25 01:35:25 2011-12-25 01:35:25
20*.11*.3.10*   6284              1 2011-12-25 01:47:45 2011-12-25 01:47:45

[680 rows x 3 columns]

# data2
                           requests               begin                 end
IP              sessionID                                                  
*8.24*.135.24*  9215              1 2011-12-29 03:14:10 2011-12-29 03:14:10
*09.2**.22*.4*  9216              1 2011-12-29 03:14:38 2011-12-29 03:14:38
*21.14*.2**.22* 9217             12 2011-12-29 03:16:06 2011-12-29 03:19:45 
...
19*.8*.2**.1*1  62728             2 2012-03-31 11:08:47 2012-03-31 11:08:47
6*.16*.10*.155  77282             1 2012-03-31 11:19:33 2012-03-31 11:19:33
17*.3*.18*.6*   77305             1 2012-03-31 11:55:52 2012-03-31 11:55:52
6*.6*.2*.20*    77308             1 2012-03-31 11:59:05 2012-03-31 11:59:05

[10411 rows x 3 columns] 

【问题讨论】:

    标签: python numpy pandas merge


    【解决方案1】:

    看起来,当您执行 data1["requests"].add(data2["requests"], fill_value=0) 时,您正在尝试将 2 个具有不同行大小的熊猫系列相加。 Series.add 将向两个系列中的所有元素广播添加操作,这意味着相同的维度。

    【讨论】:

    • 嗯,一个简单的 print data2["end"].combine_first(data1["end"]) 也失败了,所以它不仅仅是 add 语句。但是,Series.Add() 接受一个 fill_value,它应该将系列填充为相等的长度,如下所示:stackoverflow.com/questions/16202711/… ... 还是我误解了你的意思?
    • 我很确定 fill_value 用于缺失值,即具有 NA 值的单元格。但我认为它不会以同样的方式处理丢失的行。关于combine_first,是同一种错误吗?老实说,我之前从未使用过它,并且根据文档的描述,我无法想象它会出现广播错误。
    • 是的,它们(几乎)是一样的,这就是为什么我的问题看起来如此奇怪。 data1["begin"].combine_first(data2["begin"]) 产生 ValueError: operands could not be broadcast together with shapes (6482,) (2981,) (6482,) data1["begin"].combine_first(data2["begin"]) 产生 ValueError: operands could not be broadcast together with shapes (6482,) (2981,)
    【解决方案2】:

    我不知道为什么,可能是错误或其他原因,但明确声明使用 [:] 的每个系列中的所有行按预期工作。没有错误。

    print pd.concat([\
        data1["begin"][:].combine_first(data2["begin"][:]),\
        data2["end"][:].combine_first(data1["end"][:]),\
        data1["requests"][:].add(data2["requests"][:], fill_value=0)\
        ], axis=1)
    

    【讨论】:

      【解决方案3】:

      使用numpy.concatenate((df['col1', df['col2']), axis=None)) 作品。

      【讨论】:

        猜你喜欢
        • 2017-09-09
        • 2020-09-06
        • 2012-10-31
        • 2013-04-07
        • 2012-08-05
        • 2018-08-18
        • 2020-06-20
        • 1970-01-01
        • 2014-08-24
        相关资源
        最近更新 更多