如何从python groupby计数中排除NaN/NaT/None,但包括行?

2024-05-13 09:08:54 发布

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

>> df

    Foo     Bar     Number  Date
0   abc     None    NaN     NaT
1   abcdefg None    NaN     NaT
2   abcd    this    1111222 3/8/2017
3   abcd    that    1233336 3/3/2017
4   abcd    what    1346554 3/3/2017
5   abcde   that    8889995 3/9/2017
6   abcde   this    1849552 3/8/2017
7   abcd    that    7418652 3/3/2017
8   abcdef  this    4865154 3/7/2017


>>  df.groupby(['Foo']).size().reset_index(name='Total')

如果我这样做的话,这一行被算作有一个值,它确实是这样,我理解这一点。我不知道如何将该行包括在总计中,但实际上不计算None/NaN/NaT值?在

退货:

^{pr2}$

预期结果:

    Foo     Total   
0   abc     0
1   abcd    4
2   abcde   2
3   abcdef  1
4   abcdefg 0

Tags: nonenumberdfthatfoobarnanthis
1条回答
网友
1楼 · 发布于 2024-05-13 09:08:54

您可以先删除空值,然后用Foo列的唯一值在结尾处用填充值重新编制索引。在

(df.dropna().groupby('Foo')
            .size()
            .reindex(df.Foo.unique(), fill_value=0)
            .reset_index(name='total'))

或者,您可以将您的FooCategorical。在

^{pr2}$

演示

>>> (df.dropna().groupby('Foo')
                .size()
                .reindex(df.Foo.unique(), fill_value=0)
                .reset_index(name='total'))

       Foo  total
0      abc      0
1  abcdefg      0
2     abcd      4
3    abcde      2
4   abcdef      1

############################################################################

>>> df.Foo = pd.Categorical(df.Foo)

>>> df.dropna().groupby('Foo').size().reset_index(name='total')

       Foo  total
0      abc      0
1     abcd      4
2    abcde      2
3   abcdef      1
4  abcdefg      0

相关问题 更多 >