AttributeError: 'NoneType' object has no attribute 'split' in Python

Advertisements

Here are my commands that I get an error when I run:

start = datetime.now()

df_no_dup["tag_count"] = df_no_dup["Tags"].apply(lambda text: len(text.split(" ")))
# adding a new feature number of tags per question
print("Time taken to run this cell :", datetime.now() - start)
df_no_dup.head()

This is the error message I got:

----> 3 df_no_dup["tag_count"] = df_no_dup["Tags"].apply(lambda text: len(text.split(" ")))
      4 # adding a new feature number of tags per question
      5 print("Time taken to run this cell :", datetime.now() - start)

AttributeError: 'NoneType' object has no attribute 'split'

>Solution :

If you are sure that the values you are dealing with are all strings then you can make a judgment

from datetime import datetime


start = datetime.now()

df_no_dup["tag_count"] = df_no_dup["Tags"].apply(lambda text: len(text.split(" ") if text else 0))
# adding a new feature number of tags per question
print("Time taken to run this cell :", datetime.now() - start)
df_no_dup.head()

Leave a ReplyCancel reply