Jak uzyskać sumaryczną liczbę brakujących / NaN danych według kolumn w 'pandy'?

W R mogę szybko zobaczyć liczbę brakujących danych za pomocą polecenia summary, ale równoważna metoda pandas DataFrame, describe nie zgłasza tych wartości.

Myślę, że mogę zrobić coś takiego

len(mydata.index) - mydata.count()

Aby obliczyć liczbę brakujących wartości dla każdej kolumny, ale zastanawiam się, czy jest lepszy idiom (lub czy moje podejście jest w ogóle słuszne).

Author: desertnaut, 2014-03-07

5 answers

Zarówno describe jak i info zgłaszają liczbę Nie brakujących wartości.

In [1]: df = DataFrame(np.random.randn(10,2))

In [2]: df.iloc[3:6,0] = np.nan

In [3]: df
Out[3]: 
          0         1
0 -0.560342  1.862640
1 -1.237742  0.596384
2  0.603539 -1.561594
3       NaN  3.018954
4       NaN -0.046759
5       NaN  0.480158
6  0.113200 -0.911159
7  0.990895  0.612990
8  0.668534 -0.701769
9 -0.607247 -0.489427

[10 rows x 2 columns]

In [4]: df.describe()
Out[4]: 
              0          1
count  7.000000  10.000000
mean  -0.004166   0.286042
std    0.818586   1.363422
min   -1.237742  -1.561594
25%   -0.583795  -0.648684
50%    0.113200   0.216699
75%    0.636036   0.608839
max    0.990895   3.018954

[8 rows x 2 columns]


In [5]: df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 10 entries, 0 to 9
Data columns (total 2 columns):
0    7 non-null float64
1    10 non-null float64
dtypes: float64(2)

Aby uzyskać liczbę zaginionych, Twój soln jest poprawny

In [20]: len(df.index)-df.count()
Out[20]: 
0    3
1    0
dtype: int64

You could do this too

In [23]: df.isnull().sum()
Out[23]: 
0    3
1    0
dtype: int64
 46
Author: Jeff,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-07 18:23:10

Jako mały dodatek, aby uzyskać procent brakujących przez kolumnę DataFrame, połączenie powyższych odpowiedzi @ Jeff i @ userS daje Ci:

df.isnull().sum()/len(df)*100
 8
Author: Ricky McMaster,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-24 11:22:49

To nie jest pełne podsumowanie, ale da Ci szybkie wyczucie danych z poziomu Kolumny

def getPctMissing(series):
    num = series.isnull().sum()
    den = series.count()
    return 100*(num/den)
 3
Author: userS,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-22 08:21:11

Następujący po nich wykona sztuczkę i zwróci liczbę NULL dla każdej kolumny:

df.isnull().sum(axis=0)

df.isnull() zwraca ramkę danych z wartościami True / False
sum(axis=0) sumuje wartości we wszystkich wierszach dla kolumny

 2
Author: Kshitij,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-08-31 02:13:24

Jeśli nie obchodziło cię, które kolumny mają Nan ' s i chciałeś tylko sprawdzić ogólnie, dodaj chwilę .sum (), aby uzyskać pojedynczą wartość.

result = df.isnull().sum().sum()
result > 0
Seria potrzebuje tylko jednego .sum() i Panel () wymagałyby trzech
 0
Author: Drafter250,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-07-27 17:02:21