Counting the number of "NaN" (not zeros or spaces) in csv

Is it possible for Python to count the amount of "NaN" (as a string / text) in a csv file? Tried to use pandas' read_csv, but some columns with spaces are also read as NaN. The only working method I know is to use excel find 'NaN' as values.

Does anyone know of other methods? Thanks in advance!

+4
source share
3 answers

You can use pd.read_csv, but you will need two parameters: na_valuesand keep_default_na.

  • na_values:

NA/NaN. , NA. : NaN: ',' # N/A, '# N/A N/A,' #NA, '-1. # IND,' -1. # QNAN, '-NaN, '-nan,' 1. # IND, '1. # QNAN,' N/A, 'NA,' NULL, 'NaN,' nan`.

  1. keep_default_na:

na_values ​​ keep_default_na - False , NaN , .

, :

pd.read_csv('path/to/file.csv', na_values='NaN', keep_default_na=False)

"", na_values=['nan', 'NaN'] - , .

- , CSV 1 NaN :

enter image description here

import pandas as pd
import numpy as np
df = pd.read_csv('input/sample.csv', na_values='NaN', keep_default_na=False)
print(np.count_nonzero(df.isnull().values))
# 1
+5


csv tst.csv, :

h1,h2,h3
NaN,1,
2,3,NaN
5,6,9
NaN,1,
2,3,NaN
5,6,9


open str.count

with open('tst.csv') as f:
    c = f.read().count('NaN')

print(c)

4
+1
df.isna().sum()

the number of NaNs per column will be listed

0
source

Source: https://habr.com/ru/post/1687047/


All Articles