2016-09-01 11 views
4

これは私の問題表示欠落値

のは、私はこのように見えるのデータフレーム上の2列があるとしましょうです:私はすべての行方不明を表示したいと思い

Type | Killed 
_______ |________ 
Dog  1 
Dog  nan 
Dog  nan 
Cat  4 
Cat  nan 
Cow  1 
Cow  nan 

Type | Sum(isnull) 
Dog  2 
Cat  1 
Cow  1 
:タイプとに応じて殺したの値は、それらが

私の欲求の結果は次のようになります数えます

とにかくこれを表示するには?

答えて

3

あなたはvalue_countsboolean indexingを使用することができます。

print (df.ix[df.Killed.isnull(), 'Type'].value_counts().reset_index(name='Sum(isnull)')) 

    index Sum(isnull) 
0 Dog   2 
1 Cow   1 
2 Cat   1 

または集約sizeを、それが速いようだ:

print (df[df.Killed.isnull()] 
      .groupby('Type')['Killed'] 
      .size() 
      .reset_index(name='Sum(isnull)')) 

    Type Sum(isnull) 
0 Cat   1 
1 Cow   1 
2 Dog   2 

タイミング

df = pd.concat([df]*1000).reset_index(drop=True) 

In [30]: %timeit (df.ix[df.Killed.isnull(), 'Type'].value_counts().reset_index(name='Sum(isnull)')) 
100 loops, best of 3: 5.36 ms per loop 

In [31]: %timeit (df[df.Killed.isnull()].groupby('Type')['Killed'].size().reset_index(name='Sum(isnull)')) 
100 loops, best of 3: 2.02 ms per loop 
1

私はあなたの両方を得ることができますとnotnull

isnull = np.where(df.Killed.isnull(), 'isnull', 'notnull') 
df.groupby([df.Type, isnull]).size().unstack() 

enter image description here