Python 如果值是浮点值,则选择数据帧中的行 def categorizeMainUrl(url): 类别=“其他” 如果url中有“/special/”: 类别=“特殊” 退货类别 df[“category”]=df[“main_URL”]。应用(lambda URL:categorizeMainUrl(URL))

Python 如果值是浮点值,则选择数据帧中的行 def categorizeMainUrl(url): 类别=“其他” 如果url中有“/special/”: 类别=“特殊” 退货类别 df[“category”]=df[“main_URL”]。应用(lambda URL:categorizeMainUrl(URL)),python,pandas,Python,Pandas,在运行这部分代码时,我保留以下异常。 “类型错误:类型为“float”的参数不可编辑” 如何仅选择具有浮点值的数据帧部分? (在此列中,我将仅等待字符串作为数据类型)以下命令仅选择包含特定数据类型的行(此处为float): df[df[“category]”。应用(lambda x:isinstance(x,float))用于填充NaN值,然后可以与或一起使用 要创建新系列,请执行以下操作: df["category"] = np.where(df['main_URL'].fillna('').

在运行这部分代码时,我保留以下异常。
“类型错误:类型为“float”的参数不可编辑”

如何仅选择具有浮点值的数据帧部分?

(在此列中,我将仅等待字符串作为数据类型)

以下命令仅选择包含特定数据类型的行(此处为float):
df[df[“category]”。应用(lambda x:isinstance(x,float))
用于填充
NaN
值,然后可以与或一起使用 要创建新系列,请执行以下操作:

df["category"] = np.where(df['main_URL'].fillna('').str.contains('/special/'),
                          "special", "other")



我建议您查看:

此解决方案有效,但效率肯定很低,我建议您查看:
df["category"] = (df['main_URL'].fillna('')
                                .str.contains('/special/')
                                .map({True:"special",
                                      False:"other"}) 
                 )
#df['main_URL'].fillna('').str.contains('/special/').replace({True:"special",
#                                                              False:"other"})