How to count nan values in a pandas DataFrame?

前端 未结 7 1868
天涯浪人
天涯浪人 2020-12-18 18:40

What is the best way to account for (not a number) nan values in a pandas DataFrame?

The following code:

import numpy as np
import pandas as pd
dfd =         


        
7条回答
  •  情话喂你
    2020-12-18 19:09

    A good clean way to count all NaN's in all columns of your dataframe would be ...

    import pandas as pd 
    import numpy as np
    
    
    df = pd.DataFrame({'a':[1,2,np.nan], 'b':[np.nan,1,np.nan]})
    print(df.isna().sum().sum())
    

    Using a single sum, you get the count of NaN's for each column. The second sum, sums those column sums.

提交回复
热议问题