Back to questions
JuniorTheoryCommon
What does this program print?
import numpy as np
import pandas as pd
df = pd.DataFrame({
"age": [25, np.nan, 31],
"city": ["Kazan", None, None],
})
print(df.isna().sum().tolist())[1, 2]
- Expecting
[0, 2, 1], a count per row:sum()adds down each column by default, so you get one count per column — 1 forage, 2 forcity. - Expecting
Nonein a text column to go uncounted:isna()marks bothNaNandNoneas missing.
- How would you count the missing values in each row instead?
- How do you get the share of missing values per column rather than the count?