【问题标题】:Groupby/Sum in Python Pandas - zero counts not showing ...sometimesPython Pandas 中的 Groupby/Sum - 零计数不显示...有时
【发布时间】:2017-01-11 11:54:42
【问题描述】:

背景

我有一个模拟人口的数据集。它们具有以下属性

  1. 年龄(0-120 岁)
  2. 性别(男、女)
  3. 种族(白人、黑人、西班牙裔、亚裔、其他)

df.head()

   Age  Race  Gender  in_population
0   32     0       0              1
1   53     0       0              1
2   49     0       1              1
3   12     0       0              1
4   28     0       0              1

还有另一个变量将个人标识为“In_Population”*,它是一个布尔变量。我在 pandas 中使用 groupby 将人口分组为 3 个属性的可能组合,以通过对每个可能的人类别中的“In_Population”变量求和来计算计数表。

共有 2 个性别 * 5 个种族 * 121 个年龄 = 1210 个可能的群体,每个人都将属于这些群体。

如果特定年份的特定人群没有成员(例如 0 岁男性“其他”),那么我仍然希望该人群出现在我的 group-by 数据框中,但计数为零.这在下面的数据示例中正确发生(年龄 = 0,性别 = {0,1},种族 = 4)。在这个特殊的地方没有“其他”零岁儿童

grouped_obj = df.groupby( ['Age','Gender','Race'] )
groupedAGR  = grouped_obj.sum()
groupedAGR.head(10)

                 in_population
Age Gender Race               
0   0      0                16
           1                 8
           2                63
           3                 5
           4                 0
    1      0                22
           1                 4
           2                64
           3                12
           4                 0

问题

这只发生在某些年龄-性别-种族组合中。 有时,零和组会被完全跳过。以下是 45 岁的数据。我期待看到 0,表明该数据集中没有 45 岁的男性“其他”种族。

>>> groupedAGR.xs( 45, level = 'Age' )
             in_population
Gender Race               
0      0               515
       1                68
       2                40
       3                20
1      0               522
       1                83
       2                48
       3                29
       4                 3

备注

*"In_Population" 在计算“死亡率”时,基本过滤掉不属于相关人群的“新生儿”和“移民”;人口中的死亡发生在移民和出生之前,所以我将它们排除在计算之外。我怀疑这与它有关 - 零岁儿童的计数为零,但其他所有年龄组都没有显示任何东西……但事实并非如此。

>>> groupedAGR.xs( 88, level = 'Age' )
             in_population
Gender Race               
0      0                52
       2                 1
       3                 0
1      0                62
       1                 3
       2                 5
       3                 3
       4                 1

人口中没有 88 岁的亚洲男性,因此该类别中的值为零。人口中也没有 88 岁的“其他”男性,但他们根本没有出现。

编辑:我在代码中添加了显示如何在 pandas 中按对象进行分组以及如何求和以查找每个组中的计数的代码。

【问题讨论】:

    标签: python pandas group-by aggregation pandas-groupby


    【解决方案1】:

    使用带有预定义索引的reindexfill_value=0

    ages = np.arange(21, 26)
    genders = ['male', 'female']
    races = ['white', 'black', 'hispanic', 'asian', 'other']
    
    sim_size = 10000
    
    midx = pd.MultiIndex.from_product([
            ages,
            genders,
            races
        ], names=['Age', 'Gender', 'Race'])
    
    sim_df = pd.DataFrame({
            # I use [1:-1] to explicitly skip some age groups
            'Age': np.random.choice(ages[1:-1], sim_size),
            'Gender': np.random.choice(genders, sim_size),
            'Race': np.random.choice(races, sim_size)
        })
    

    这些将缺少年龄组

    counts = sim_df.groupby(sim_df.columns.tolist()).size()
    counts.unstack()
    

    这填补了缺失的年龄组

    counts.reindex(midx, fill_value=0).unstack()
    

    【讨论】:

    • 谢谢!那行得通!有没有办法使输出一维?我只想查看一列计数。
    • 另外,你有什么直觉为什么零只会偶尔出现吗?而且人口中的“零计数”子集根本不会出现?
    • 我喜欢你通过 imgur 链接发布图片的方式。我打开了“编辑”功能,看看你是怎么做到的。很酷。
    • @NirvanSengupta 移除 unstack 以获得 1 d
    猜你喜欢
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 2019-01-03
    • 2020-07-12
    • 2017-03-16
    • 2020-05-12
    • 1970-01-01
    相关资源
    最近更新 更多