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

huangapple go评论73阅读模式
英文:

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

问题

以下是您运行时出现错误的命令:

start = datetime.now()

df_no_dup["tag_count"] = df_no_dup["Tags"].apply(lambda text: len(text.split(" ")))
# 添加一个新的特征:每个问题的标签数量
print("运行此单元格所花费的时间:", datetime.now() - start)
df_no_dup.head()

这是我收到的错误消息:

----> 3 df_no_dup["tag_count"] = df_no_dup["Tags"].apply(lambda text: len(text.split(" ")))
      4 # 添加一个新的特征:每个问题的标签数量
      5 print("运行此单元格所花费的时间:", datetime.now() - start)

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

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'

答案1

得分: 0

如果您确定您正在处理的值都是字符串,那么您可以做出判断。

from datetime import datetime

# 获取当前时间
start = datetime.now()

# 使用lambda函数计算每个问题中标签的数量,并将结果存储在新列"tag_count"中
df_no_dup["tag_count"] = df_no_dup["Tags"].apply(lambda text: len(text.split(" ")) if text else 0)

# 打印运行时间
print("运行此代码块所用时间:", datetime.now() - start)

# 显示DataFrame的前几行
df_no_dup.head()
英文:

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()

答案2

得分: 0

df_no_dup["Tags"]的值为None。只需添加一个if条件来保护它。

start = datetime.now()

if df_no_dup["Tags"] is not None:
    df_no_dup["tag_count"] = df_no_dup["Tags"].apply(lambda text: len(text.split(" ")))
# 添加一个新特征,每个问题的标签数量
print("运行此单元格所花费的时间:", datetime.now() - start)
df_no_dup.head()
英文:

df_no_dup["Tags"] is having None value. Just add an if clause to guard it.

    start = datetime.now()

    if df_no_dup["Tags"] is not None:
        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()

huangapple
  • 本文由 发表于 2023年5月7日 20:03:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定