将列添加到使用groupby计算不同列计数的df

2024-05-14 19:33:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在df中创建一个新列。我希望新列等于每个唯一'mother_ID的行数,这是df中的不同列

这就是我目前正在做的。它创建了新列,但新列中填充了“NaN”

df.columns = ['mother_ID', 'date_born', 'mother_mass_g', 'hatchling_masses_g'] 
df.to_numpy()

这是我打印原始df时的显示方式:

how df appears when I print it


count = df.groupby('mother_ID').hatchling_masses_g.count() 
df['count']= count

下面的图片显示了我打印新df时得到的结果,尽管如果我只是print(count),我会得到每个mother_ID的正确计数。有人知道我做错了什么吗

enter image description here


Tags: columnstonumpyiddfdatecount图片
1条回答
网友
1楼 · 发布于 2024-05-14 19:33:45

使用^{}

df['count'] = df.groupby('mother_ID')['hatchling_masses_g'].transform('count')

注意^{}^{}'count'之间的区别

样本数据:

import numpy as np
import pandas as pd

np.random.seed(5)
df = pd.DataFrame({
    'mother_ID': np.random.choice(['a', 'b'], 10),
    'hatchling_masses_g': np.random.randint(1, 100, 10)
})
  mother_ID  hatchling_masses_g
0         b                  63
1         a                  28
2         b                  31
3         b                  81
4         a                   8
5         a                  77
6         a                  16
7         b                  54
8         a                  81
9         a                  28

groupby.count

counts = df.groupby('mother_ID')['hatchling_masses_g'].count()
mother_ID
a    6
b    4
Name: hatchling_masses_g, dtype: int64

请注意,只有两行。当分配回数据帧时,有10行,这意味着pandas不知道如何将数据重新对齐。这将导致NaNs表示缺少数据:

df['count'] = counts
  mother_ID  hatchling_masses_g  count
0         b                  63    NaN
1         a                  28    NaN
2         b                  31    NaN
3         b                  81    NaN
4         a                   8    NaN
5         a                  77    NaN
6         a                  16    NaN
7         b                  54    NaN
8         a                  81    NaN
9         a                  28    NaN

它试图在索引中找到'a'和'b',因为它无法找到,所以它只能用NaN值填充


groupby.tranform('count')

另一方面transform将使用以下计数填充整个组:

counts = df.groupby('mother_ID')['hatchling_masses_g'].transform('count')

counts

0    4
1    6
2    4
3    4
4    6
5    6
6    6
7    4
8    6
9    6
Name: hatchling_masses_g, dtype: int64

注意,创建了10行(数据框中每行一行):

这将很好地分配回数据帧(因为索引对齐):

df['count'] = counts
  mother_ID  hatchling_masses_g  count
0         b                  63      4
1         a                  28      6
2         b                  31      4
3         b                  81      4
4         a                   8      6
5         a                  77      6
6         a                  16      6
7         b                  54      4
8         a                  81      6
9         a                  28      6

如果需要,可以通过^{}进行计数,然后^{}返回到组键上的数据帧:

counts = df.groupby('mother_ID')['hatchling_masses_g'].count().rename('count')
df = df.join(counts, on='mother_ID')

counts

mother_ID
a    6
b    4
Name: count, dtype: int64

df

  mother_ID  hatchling_masses_g  count
0         b                  63      4
1         a                  28      6
2         b                  31      4
3         b                  81      4
4         a                   8      6
5         a                  77      6
6         a                  16      6
7         b                  54      4
8         a                  81      6
9         a                  28      6

相关问题 更多 >

    热门问题