1

我是 python 新手。这是我的问题,这对我来说真的很奇怪。

一个简单的数据框如下所示:

a1=pd.DataFrame({'Hash':[1,1,2,2,2,3,4,4],
                 'Card':[1,1,2,2,3,3,4,4]})

我需要将 a1 按哈希分组,计算每组有多少行,然后在 a1 中添加一列来表示行号。所以,我想使用 groupby + transform。

当我使用:

a1['CustomerCount']=a1.groupby(['Hash']).transform(lambda x: x.shape[0])

结果是正确的:

   Card  Hash  CustomerCount
0     1     1              2
1     1     1              2
2     2     2              3
3     2     2              3
4     3     2              3
5     3     3              1
6     4     4              2
7     4     4              2

但是当我使用:

a1.loc[:,'CustomerCount']=a1.groupby(['Hash']).transform(lambda x: x.shape[0])

结果是:

   Card  Hash  CustomerCount
0     1     1            NaN
1     1     1            NaN
2     2     2            NaN
3     2     2            NaN
4     3     2            NaN
5     3     3            NaN
6     4     4            NaN
7     4     4            NaN

那么,为什么会发生这种情况?

据我所知,loc 和 iloc(如 a1.loc[:,'CustomerCount'])总比没有好(如 a1['CustomerCount']),因此通常建议使用 loc 和 iloc。但是为什么会这样呢?

此外,我多次尝试使用 loc 和 iloc 在一个数据框中生成一个新列。他们通常工作。那么这和groupby + transform有关系吗?

4

1 回答 1

3

不同之处在于如何loc处理将DataFrame对象分配给单个列。当您分配它DataFrame的列时,Card它试图排列索引和列名。列没有排列,你得到了NaNs。通过直接列访问分配时,它确定它是另一列的一列,然后就这样做了。

减少到单列

您可以通过将操作结果减少groupby到仅一列来解决此问题,从而轻松解决问题。

a1.loc[:,'CustomerCount'] = a1.groupby(['Hash']).Card.transform('size')
a1

   Hash  Card  CustomerCount
0     1     1              2
1     1     1              2
2     2     2              3
3     2     2              3
4     2     3              3
5     3     3              1
6     4     4              2
7     4     4              2

重命名列

不要真的这样做,其他答案要简单得多

a1.loc[:, 'CustomerCount'] = a1.groupby('Hash').transform(len).rename(
    columns={'Card': 'CustomerCount'})
a1

pd.factorizenp.bincount

我实际上会做什么

f, u = pd.factorize(a1.Hash)
a1['CustomerCount'] = np.bincount(f)[f]
a1

或内联制作副本

a1.assign(CustomerCount=(lambda f: np.bincount(f)[f])(pd.factorize(a1.Hash)[0]))

   Hash  Card  CustomerCount
0     1     1              2
1     1     1              2
2     2     2              3
3     2     2              3
4     2     3              3
5     3     3              1
6     4     4              2
7     4     4              2
于 2018-06-05T03:41:13.690 回答