So überprüfen Sie, ob ein Wert in einem Pandas DataFrame NaN ist
You can use the isna()
method to check for NaN values in a Pandas DataFrame. The method returns a DataFrame of the same shape as the original, but with True
or False
values indicating whether each element is NaN or not. Here is an example code snippet:
import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame({
'A': [1, 2, 3, 4, float('nan')],
'B': [float('nan'), 2, 3, 4, 5],
'C': [1, 2, float('nan'), 4, 5]
})
# Check for NaN values
nan_mask = df.isna()
# Print the result
print(nan_mask)
You can also use isnull()
method for the same purpose
nan_mask = df.isnull()
You can also use the following code snippet to check if any value is NaN in the DataFrame:
if df.isna().any().any():
print("Dataframe contains NaN values")
else:
print("Dataframe contains no NaN values")
or
if df.isnull().any().any():
print("Dataframe contains NaN values")
else:
print("Dataframe contains no NaN values")