pandas 在groupby函数中计算空值 [英] Pandas count null values in a groupby function
本文介绍了 pandas 在groupby函数中计算空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],
'B' : ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'],
'C' : [np.nan, 'bla2', np.nan, 'bla3', np.nan, np.nan, np.nan, np.nan]})
输出:
A B C
0 foo one NaN
1 bar one bla2
2 foo two NaN
3 bar three bla3
4 foo two NaN
5 bar two NaN
6 foo one NaN
7 foo three NaN
我想使用groupby来计算foo的不同组合的NaN数.
I would like to use groupby in order to count the number of NaN's for the different combinations of foo.
预期输出(EDIT):
Expected Output (EDIT):
A B C D
0 foo one NaN 2
1 bar one bla2 0
2 foo two NaN 2
3 bar three bla3 0
4 foo two NaN 2
5 bar two NaN 1
6 foo one NaN 2
7 foo three NaN 1
当前我正在尝试:
df['count']=df.groupby(['A'])['B'].isnull().transform('sum')
但这不起作用...
谢谢
推荐答案
我认为您需要如果需要过滤器,请先添加 boolean indexing
:
If need filter first add boolean indexing
:
df = df[df['A'] == 'foo']
df2 = df.C.isnull().groupby([df['A'],df['B']]).sum().astype(int)
print (df2)
A B
foo one 2
three 1
two 2
或更简单:
df = df[df['A'] == 'foo']
df2 = df['B'].value_counts()
print (df2)
one 2
two 2
three 1
Name: B, dtype: int64
Solution is very similar, only add transform
:
df['D'] = df.C.isnull().groupby([df['A'],df['B']]).transform('sum').astype(int)
print (df)
A B C D
0 foo one NaN 2
1 bar one bla2 0
2 foo two NaN 2
3 bar three bla3 0
4 foo two NaN 2
5 bar two NaN 1
6 foo one NaN 2
7 foo three NaN 1
类似的解决方案:
df['D'] = df.C.isnull()
df['D'] = df.groupby(['A','B'])['D'].transform('sum').astype(int)
print (df)
A B C D
0 foo one NaN 2
1 bar one bla2 0
2 foo two NaN 2
3 bar three bla3 0
4 foo two NaN 2
5 bar two NaN 1
6 foo one NaN 2
7 foo three NaN 1
这篇关于 pandas 在groupby函数中计算空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文