简体   繁体   English

熊猫df差异数据必须为一维

[英]Pandas df difference data must be 1-dimensional

I am trying to calculate the difference in item price for combinations of shop and item like this: 我正在尝试计算商店和商品组合的商品价格差异,如下所示:

index_cols = ['shop_id', 'item_id', 'date_block_num']

#get aggregated values for (shop_id, item_id, month)
gb = sales.groupby(index_cols).agg({'item_cnt_day':[np.sum], 'item_price':[np.mean]}).reset_index()\
.rename(columns={'sum': 'item_cnt_month','mean':'item_price'})

gb['diff'] = gb.groupby(['shop_id','item_id'])['item_price'].transform(np.diff).fillna(0)

gb

As you can see I am trying to use np.diff (from numpy) for faster computation buy I am getting the following error: 如您所见,我正在尝试使用np.diff(来自numpy)进行更快的计算,我得到以下错误:

Exception: Data must be 1-dimensional 例外:数据必须是一维的

EDIT: 编辑:

Data Sample: 数据样本:

            shop_id   item_id date_block_num item_cnt_day item_price
            0         30      1              31.0         265.0
            0         31      1              11.0         434.0
            0         32      0              6.0          221.0
            0         32      1              10.0         221.0
            0         33      0              3.0          347.0
            59        22164   27             2.0          699.0
            59        22164   30             1.0          699.0
            59        22167   9              1.0          299.0
            59        22167   11             2.0          299.0
            59        22167   17             1.0          299.0

Any idea to avoid this error while using numpy or a faster way to do it? 有什么想法可以避免在使用numpy或更快的方法时发生此错误? Thanks. 谢谢。

Remove one element lists for [np.mean] and [np.sum] to np.mean and np.sum for prevent MultiIndex in columns: 删除[np.mean][np.sum]np.meannp.sum一个元素列表,以防止列中出现MultiIndex

gb = (sales.groupby(index_cols)
           .agg({'item_cnt_day':np.sum, 'item_price':np.mean})
           .reset_index()
           .rename(columns={'sum': 'item_cnt_month','mean':'item_price'}))

Then is possible use (but not 100% sure if better performance): 然后可以使用(但不能百分百确定是否有更好的性能):

gb['diff'] = gb.groupby(['shop_id','item_id'])['item_price'].diff()

EDIT: 编辑:

Data sample: 数据样本:

index_cols = ['shop_id', 'item_id', 'date_block_num']

sales = pd.DataFrame({
        'item_id':list('aaaaaa'),
         'shop_id':list('aaabbb'),
         'date_block_num':[4,5,4,5,5,4],
         'item_cnt_day':[7,8,9,4,2,3],
         'item_price':[1,3,5,7,1,0]

})

gb = (sales.groupby(index_cols)
           .agg({'item_cnt_day':[np.sum], 'item_price':[np.mean]})
           .reset_index()
           .rename(columns={'sum': 'item_cnt_month','mean':'item_price'}))
print (gb)
  shop_id item_id date_block_num   item_cnt_day item_price
                                 item_cnt_month item_price
0       a       a              4             16          3
1       a       a              5              8          3
2       b       a              4              3          0
3       b       a              5              6          4

gb = (sales.groupby(index_cols)
           .agg({'item_cnt_day':np.sum, 'item_price':np.mean})
           .reset_index()
           .rename(columns={'sum': 'item_cnt_month','mean':'item_price'}))
print (gb)

  shop_id item_id  date_block_num  item_cnt_day  item_price
0       a       a               4            16           3
1       a       a               5             8           3
2       b       a               4             3           0
3       b       a               5             6           4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM