Groupby class and count missing values in features

前端 未结 3 886
慢半拍i
慢半拍i 2021-01-17 07:16

I have a problem and I cannot find any solution in the web or documentation, even if I think that it is very trivial.

What do I want to do?

I have a datafram

3条回答
  •  礼貌的吻别
    2021-01-17 08:14

    Using the diff between count and size

    g=df.groupby('CLASS')
    
    -g.count().sub(g.size(),0)
    
              FEATURE1  FEATURE2  FEATURE3
    CLASS                              
    B             0         0         0
    X             1         1         2
    

    And we can transform this question to the more generic question how to count how many NaN in dataframe with for loop

    pd.DataFrame({x: y.isna().sum()for x , y in g }).T.drop('CLASS',1)
    Out[468]: 
       FEATURE1  FEATURE2  FEATURE3
    B         0         0         0
    X         1         1         2
    

提交回复
热议问题