Uwe.Schneider
Uwe.Schneider

Reputation: 1415

Check pandas for NaN and differing types

In order to check, whether a pandas contains missing/nan values, one can use the isnull function.

test_pandas = pd.DataFrame([[np.float(3),np.float(1),np.float(4.3)],[np.float(5.8),np.nan,[1,2,3]]],columns = ['A','B','C'])
value = test_pandas.isnull().values.any()
test_pandas.head()

gives

     A    B          C
0  3.0  1.0        4.3
1  5.8  NaN  [1, 2, 3]

and with

print("There exists a nan value in the dataframe: ",test_pandas.isnull().values.any())
print("Number of nan values: ",test_pandas.isnull().sum().sum())

we find

There exists a nan value in the dataframe:  True
Number of nan values:  1

Upvotes: 0

Views: 65

Answers (1)

Dani Mesejo
Dani Mesejo

Reputation: 61900

You could use a custom function with applymap:

def isfloat(x):
    return isinstance(x, float)


print(df.applymap(isfloat))

Output

      A     B      C
0  True  True   True
1  True  True  False

Upvotes: 3

Related Questions