[英]How to solve the Attribute error 'float' object has no attribute 'split' in python?
When I run the below code, it gives me an error saying that there is attribute error: 'float' object has no attribute 'split' in python.当我运行下面的代码时,它给我一个错误,说存在属性错误:'float' object has no attribute 'split' in python。
I would like to know why this error comes about.我想知道为什么会出现这个错误。
def text_processing(df):
"""""=== Lower case ==="""
'''First step is to transform comments into lower case'''
df['content'] = df['content'].apply(lambda x: " ".join(x.lower() for x in x.split() if x not in stop_words))
return df
df = text_processing(df)
The full traceback for the error:错误的完整回溯:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.2.2\helpers\pydev\pydevd.py", line 1664, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.2.2\helpers\pydev\pydevd.py", line 1658, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.2.2\helpers\pydev\pydevd.py", line 1068, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.2.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/L31307/Documents/FYP P3_Lynn_161015H/FYP 10.10.18 (Wed) still working on it/FYP/dataanalysis/category_analysis.py", line 53, in <module>
df = text_processing(df)
File "C:/Users/L31307/Documents/FYP P3_Lynn_161015H/FYP 10.10.18 (Wed) still working on it/FYP/dataanalysis/category_analysis.py", line 30, in text_processing
df['content'] = df['content'].apply(lambda x: " ".join(x.lower() for x in x.split() if x not in stop_words))
File "C:\Users\L31307\AppData\Roaming\Python\Python37\site-packages\pandas\core\series.py", line 3194, in apply
mapped = lib.map_infer(values, f, convert=convert_dtype)
File "pandas/_libs/src\inference.pyx", line 1472, in pandas._libs.lib.map_infer
File "C:/Users/L31307/Documents/FYP P3_Lynn_161015H/FYP 10.10.18 (Wed) still working on it/FYP/dataanalysis/category_analysis.py", line 30, in <lambda>
df['content'] = df['content'].apply(lambda x: " ".join(x.lower() for x in x.split() if x not in stop_words))
AttributeError: 'float' object has no attribute 'split'
The error points to this line:错误指向这一行:
df['content'] = df['content'].apply(lambda x: " ".join(x.lower() for x in x.split() \
if x not in stop_words))
split
is being used here as a method of Python's built-in str
class. split
在这里用作 Python 内置str
类的方法。 Your error indicates one or more values in df['content']
is of type float
.您的错误表明
df['content']
中的一个或多个值是float
类型。 This could be because there is a null value, ie NaN
, or a non-null float value.这可能是因为存在空值,即
NaN
或非空浮点值。
One workaround, which will stringify floats, is to just apply str
on x
before using split
:将字符串化浮点数的一种解决方法是在使用
split
之前仅在x
上应用str
:
df['content'] = df['content'].apply(lambda x: " ".join(x.lower() for x in str(x).split() \
if x not in stop_words))
Alternatively, and possibly a better solution, be explicit and use a named function with a try
/ except
clause:或者,可能是更好的解决方案,明确并使用带有
try
/ except
子句的命名函数:
def converter(x):
try:
return ' '.join([x.lower() for x in str(x).split() if x not in stop_words])
except AttributeError:
return None # or some other value
df['content'] = df['content'].apply(converter)
Since pd.Series.apply
is just a loop with overhead, you may find a list comprehension or map
more efficient:由于
pd.Series.apply
只是一个有开销的循环,您可能会发现列表理解或map
更有效:
df['content'] = [converter(x) for x in df['content']]
df['content'] = list(map(converter, df['content']))
split() is a python method which is only applicable to strings. split() 是一种仅适用于字符串的 Python 方法。 It seems that your column "content" not only contains strings but also other values like floats to which you cannot apply the .split() mehthod.
似乎您的“内容”列不仅包含字符串,还包含其他值,例如无法应用 .split() 方法的浮点数。
Try converting the values to a string by using str(x).split() or by converting the entire column to strings first, which would be more efficient.尝试使用 str(x).split() 将值转换为字符串,或者首先将整个列转换为字符串,这样效率会更高。 You do this as follows:
您可以按如下方式执行此操作:
df['column_name'].astype(str)
Had the same issue ('float' object has no attribute 'split'), this is how I managed it:有同样的问题('float' object 没有属性'split'),这就是我管理它的方式:
Initial code...:初始代码...:
df = pd.read_excel("Forbes Athlete List 2012-2019.xlsx")
df.Pay = df.Pay.apply(lambda x: float(x.split(" ")[0].split("$")[1]))
...resulted in error: 'float' object has no attribute 'split' ...导致错误:'float' object 没有属性 'split'
So I changed the code to this:所以我把代码改成这样:
df.Pay = df.Pay.apply(lambda x: float(x.split(" ")[0].split("$")[1] if type (x) == str else str (x)))
Here is the second example of the same thing:这是同一件事的第二个例子:
Initial code that shows error:显示错误的初始代码:
df.Endorsements = df.Endorsements.apply(lambda x: float(x.split(" ")[0].split("$")[1]))
Amended code that works fine:修改后的代码可以正常工作:
df.Endorsements = df.Endorsements.apply (lambda x: float(x.split(" ")[0].split("$")[1] if type (x) == str else str (x)))
So, you guys having this problem could try adding 'if type (x) == str else str (x)' part in your code, might solve your problem.因此,遇到此问题的人可以尝试在代码中添加 'if type (x) == str else str (x)' 部分,可能会解决您的问题。
Cheers.干杯。 A.
一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.