首页 > 解决方案 > ValueError:错误的项目数通过 2,位置意味着 1 在对两列进行分组并转换组以计算计数时

问题描述

我有一个 Pandas DataFrame,如下所示:

 df =

 user_id  item_id  time  location
  u1      i1        t1.   l1
  u2      i1        t2    l2
  u1      i2        t3    l1
  u3      i2        t4    l2
  u4      i1        t5    l1
  u5      i1        t6    l1

预期输出:

  df =
 user_id  item_id  time  location count
  u1      i1       t1.   l1         3
  u2      i1       t2    l2         1
  u1      i2       t3    l1         1
  u3      i2       t4    l2         1
  u4      i1       t5    l1         3
  u5      i1       t6    l1         3

我只是想按 itemid 和 location 分组并计算每个组出现的次数。

这是有效的代码:

 df.groupby(['item_id', 'location']).size()

但是,我想将此分组附加到 df:

所以,我做了以下事情来实现这一点:

  data.groupby(['item_id', 'customer_zipcode'])['user_id','time'].transform('size')

但是,我收到以下错误:

 IndexError: Column(s) ['user_id', 'time'] already selected

然后,我这样做了:

 data.groupby(['item_id', 'location'])['user_id','time'].transform('count')

它有效,但没有提供所需的输出。

我也试过这个:

   data.groupby(['item_id', 'location']).transform('sum')

但是,这给出了一个不同的错误:

   TypeError: unsupported operand type(s) for +: 'Timestamp' and 'Timestamp'

那么,如何按两列分组,计算出现次数(计数)并将其附加回数据框?

标签: python-3.xpandaspandas-groupby

解决方案


groupby对我来说,如果在需要一个新列之后只选择一个值:

data['count1'] = data.groupby(['item_id', 'location'])['user_id'].transform('size')
data['count2'] = data.groupby(['item_id', 'location'])['user_id'].transform('count')
print (data)
  user_id item_id time location  count1  count2
0      u1      i1  t1.       l1       3       3
1      u2      i1   t2       l2       1       1
2      u1      i2   t3       l1       1       1
3      u3      i2   t4       l2       1       1
4      u4      i1   t5       l1       3       3
5      u5      i1   t6       l1       3       3

此方法之间存在差异 -size仅计算组,但count用于计算具有 excludeNaN的列的值,因此适用于测试多个列:

#no missing values, same output
data[['count2','count3']] = data.groupby(['item_id', 'location'])[['user_id', 'time']].transform('count')
print (data)
  user_id item_id time location  count2  count3
0      u1      i1  t1.       l1       3       3
1      u2      i1   t2       l2       1       1
2      u1      i2   t3       l1       1       1
3      u3      i2   t4       l2       1       1
4      u4      i1   t5       l1       3       3
5      u5      i1   t6       l1       3       3

print (data)
  user_id item_id time location
0      u1      i1  t1.       l1
1      u2      i1  NaN       l2
2      u1      i2   t3       l1
3     NaN      i2   t4       l2
4     NaN      i1   t5       l1
5      u5      i1   t6       l1

#tested each column separately
data[['count2','count3']] = data.groupby(['item_id', 'location'])[['user_id', 'time']].transform('count')
print (data)
  user_id item_id time location  count2  count3
0      u1      i1  t1.       l1       2       3
1      u2      i1  NaN       l2       1       0
2      u1      i2   t3       l1       1       1
3     NaN      i2   t4       l2       0       1
4     NaN      i1   t5       l1       2       3
5      u5      i1   t6       l1       2       3

如果使用 if fail 测试多列size,我猜是错误(或者有人注意到测试多列没有意义,因为不排除 NaN,所以所有列总是具有相同的值):

data[['count2','count3']] = data.groupby(['item_id', 'location'])[['user_id', 'time']].transform('size')

print (data)

IndexError: 列 ['user_id', 'time'] 已选择

因为如果单独使用每一列,可能会出现错误:

data['count2'] = data.groupby(['item_id', 'location'])['user_id'].transform('size')
data['count3'] = data.groupby(['item_id', 'location'])[ 'time'].transform('size')
print (data)

  user_id item_id time location  count2  count3
0      u1      i1  t1.       l1       3       3
1      u2      i1  NaN       l2       1       1
2      u1      i2   t3       l1       1       1
3     NaN      i2   t4       l2       1       1
4     NaN      i1   t5       l1       3       3
5      u5      i1   t6       l1       3       3

推荐阅读