熊猫适用,'float'对象不可迭代

问题描述 投票:0回答:1

我正在尝试从URL中提取包含名称的字符串。我编写了一个函数,该函数能够从字符串中剥离所需的字符。

def get_name(url):
name = ''.join(filter(str.isalpha, url))
name = ''.join(ch for ch in name if not ch.isupper())
return name

但是,当我在数据框中的列上使用apply时,我得到一个'float'对象不是可重复的错误。列中的每个条目均为str类型。

df['names'] = df.get('LI  URL').apply(get_name)

TypeError                                 Traceback (most recent call last)

in----> 1 df ['names'] = df.get('LI URL')。apply(get_name)

// anaconda3 / lib / python3.7 / site-packages / pandas / core / series.py in apply(self,func,convert_dtype,args,** kwds)3589其他:3590个值= self.astype(object).values-> 3591映射= lib.map_infer(值,f,convert = convert_dtype)35923593 if len(mapped)and isinstance(mapped [0],Series):

pandas._libs.lib.map_infer()中的pandas / _libs / lib.pyx

在get_name(url)中1 def get_name(url):----> 2名称=''.join(filter(str.isalpha,url))3 name =''.join(如果不是ch.isupper(),则为名称中的ch)4返回名称

TypeError:'float'对象不可迭代

Here is the column in question

我该如何解决?

python pandas apply
1个回答
0
投票

首先通过df['LI URL'].isnull().any()检查网址是否包含任何null / nan值因为似乎那里存在一些空值。

© www.soinside.com 2019 - 2024. All rights reserved.