0
0
Pandasdata~10 mins

NaN and None in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - NaN and None in Pandas
Create DataFrame with None and NaN
Pandas stores None as NaN in float columns
Check for missing values using isna()
Operations handle NaN and None similarly
Fill or drop missing values as needed
Result DataFrame
Shows how pandas treats None and NaN as missing values and how to detect and handle them.
Execution Sample
Pandas
import pandas as pd
import numpy as np
data = {'A': [1, None, 3], 'B': [4, np.nan, 6]}
df = pd.DataFrame(data)
df.isna()
Creates a DataFrame with None and NaN, then checks which values are missing.
Execution Table
StepActionDataFrame 'df'isna() Output
1Create DataFrame with None and NaN{'A': [1, None, 3], 'B': [4, nan, 6]}N/A
2Pandas converts None to NaN in column 'A'{'A': [1.0, NaN, 3.0], 'B': [4.0, NaN, 6.0]}N/A
3Call df.isna() to detect missing valuesSame as step 2{'A': [False, True, False], 'B': [False, True, False]}
4Use fillna() to replace NaN with 0Same as step 2N/A
5After fillna(0), missing values replaced{'A': [1.0, 0.0, 3.0], 'B': [4.0, 0.0, 6.0]}N/A
6Drop rows with missing values using dropna()Same as step 2N/A
7After dropna(), rows with NaN removed{'A': [1.0, 3.0], 'B': [4.0, 6.0]}N/A
8End of executionFinal DataFrame after operationsFinal isna() output if checked
💡 All missing values detected and handled; execution ends after fill or drop operations.
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 7
df{'A': [1, None, 3], 'B': [4, nan, 6]}{'A': [1.0, NaN, 3.0], 'B': [4.0, NaN, 6.0]}{'A': [1.0, 0.0, 3.0], 'B': [4.0, 0.0, 6.0]}{'A': [1.0, 3.0], 'B': [4.0, 6.0]}
isna_outputN/AN/A{'A': [False, False, False], 'B': [False, False, False]}N/A
Key Moments - 3 Insights
Why does pandas convert None to NaN in the DataFrame?
Pandas uses NaN to represent missing values in numeric columns because None is a Python object and cannot be stored in numeric arrays. This is shown in execution_table step 2 where None becomes NaN.
Does isna() detect both None and NaN as missing values?
Yes, isna() treats both None and NaN as missing values. In the execution_table step 3, both None (converted to NaN) and NaN are detected as True.
What happens if you try to fill missing values with fillna()?
fillna() replaces all NaN (and None converted to NaN) with the specified value. See execution_table step 5 where NaNs become 0.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 3, what is the isna() output for df['A'][1]?
AFalse
BTrue
CNone
D0
💡 Hint
Check the 'isna() Output' column at step 3 for column 'A' row 1.
At which step does the DataFrame replace missing values with 0?
AStep 5
BStep 7
CStep 3
DStep 2
💡 Hint
Look at the 'Action' column describing fillna() usage.
If you drop rows with missing values, what happens to the number of rows in df?
AIt stays the same
BIt increases
CIt decreases
DIt becomes zero
💡 Hint
See execution_table step 7 where dropna() removes rows with NaN.
Concept Snapshot
NaN and None in Pandas:
- None is converted to NaN in numeric columns.
- Both represent missing data.
- Use df.isna() to detect missing values.
- Use fillna() to replace missing values.
- Use dropna() to remove missing data rows.
Full Transcript
This visual execution shows how pandas handles missing data using None and NaN. When creating a DataFrame with None and NaN, pandas converts None to NaN in numeric columns. The isna() function detects both as missing values. You can replace missing values using fillna() or remove rows with missing data using dropna(). The variable tracker shows how the DataFrame changes after each operation. Key moments clarify why None becomes NaN and how missing values are detected and handled. The quiz tests understanding of these steps.