hi I have a dataframe,
df,
Name
Raj
NaN
Ravi,kumar
I am trying to get the count of words in df["Name"],
I tried, df['count']=df['Names'].str.count(',') + 1
but I am getting "1" for NaN field, how to resolve this I am getting
Name count
Raj 1
NaN 1
Ravi,kumar 2
but my desired output should be,
Name count
Raj 1
NaN
Ravi,kumar 2
It looks like NaN
is a string here. Try doing this:
df['Name'].replace('NaN', np.nan).str.count(',') + 1
You should get something along these lines:
0 1.0
1 NaN
2 2.0
Name: Name, dtype: float64
You can then call .fillna('')
to fill in the blanks.