Count NaN per row with Pandas

IIUC, this should fulfill your needs.

nasum=df['First_Name'].isnull().sum()
df['countNames'] = df.groupby('First_Name')['First_Name'].transform('count').replace(np.nan,nasum)

or, as suggested by ALollz, below code will also provide the same result

df['countNames'] = df.groupby('First_Name')['First_Name'].transform('count').fillna(nasum)

Input

       First_Name   Favorite_Color
0         Jared     Blue
1          Lily     Blue
2         Sarah     Pink
3          Bill     Red
4          Bill     Yellow
5          Alfred   Orange
6          None     Red
7          None     Pink

Output

     First_Name     Favorite_Color  countNames
0         Jared          Blue        1.0
1         Lily           Blue        1.0
2         Sarah          Pink        1.0
3         Bill           Red         2.0
4         Bill           Yellow      2.0
5         Alfred         Orange      1.0
6         None           Red         2.0
7         None           Pink        2.0

Try:

df['countNames'] = df.fillna(-1).groupby('First_Name')['First_Name'].transform('count')

First_Name Favorite_Color  countNames
0      Jared           Blue           1
1       Lily           Blue           1
2      Sarah           Pink           1
3       Bill            Red           2
4       Bill         Yellow           2
5     Alfred         Orange           1
6       None            Red           1

Tags:

Python

Pandas