当我运行下面的代码时,它给了我一个错误,说存在属性错误:"float"对象在python中没有属性"split"。
我想知道为什么会出现此错误。
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)
错误的完整回溯:
Traceback (most recent call last):
File "C:Program FilesJetBrainsPyCharm Community Edition 2018.2.2helperspydevpydevd.py", line 1664, in <module>
main()
File "C:Program FilesJetBrainsPyCharm Community Edition 2018.2.2helperspydevpydevd.py", line 1658, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:Program FilesJetBrainsPyCharm Community Edition 2018.2.2helperspydevpydevd.py", line 1068, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:Program FilesJetBrainsPyCharm Community Edition 2018.2.2helperspydev_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:UsersL31307AppDataRoamingPythonPython37site-packagespandascoreseries.py", line 3194, in apply
mapped = lib.map_infer(values, f, convert=convert_dtype)
File "pandas/_libs/srcinference.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'
错误指向以下行:
df['content'] = df['content'].apply(lambda x: " ".join(x.lower() for x in x.split()
if x not in stop_words))
split
在这里用作 Python 内置str
类的方法。您的错误指示df['content']
中的一个或多个值的类型为float
。这可能是因为有一个空值,即NaN
或非空浮点值。
一种将字符串化浮点数的解决方法是在使用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))
或者,也可能是更好的解决方案,明确并使用带有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)
由于pd.Series.apply
只是一个有开销的循环,你可能会发现列表理解或map
更有效:
df['content'] = [converter(x) for x in df['content']]
df['content'] = list(map(converter, df['content']))
split(( 是一个仅适用于字符串的 python 方法。似乎您的列"content"不仅包含字符串,还包含其他值,例如浮点数,您无法对其应用 .split(( mehthod。
尝试使用 str(x(.split(( 或先将整个列转换为字符串将值转换为字符串,这样会更有效。您可以按如下方式执行此操作:
df['column_name'].astype(str)
有同样的问题("float"对象没有属性"split"(,这就是我管理它的方式:
初始代码...:
df = pd.read_excel("Forbes Athlete List 2012-2019.xlsx")
df.Pay = df.Pay.apply(lambda x: float(x.split(" ")[0].split("$")[1]))
。导致错误:"float"对象没有属性"拆分">
所以我将代码更改为:
df.Pay = df.Pay.apply(lambda x: float(x.split(" ")[0].split("$")[1] if type (x) == str else str (x)))
这是同一件事的第二个例子:
显示错误的初始代码:
df.Endorsements = df.Endorsements.apply(lambda x: float(x.split(" ")[0].split("$")[1]))
修改后的代码工作正常:
df.Endorsements = df.Endorsements.apply (lambda x: float(x.split(" ")[0].split("$")[1] if type (x) == str else str (x)))
因此,遇到此问题的你们可以尝试在代码中添加"if type (x( == str else str (x("部分,可能会解决您的问题。
干杯! 一个。
有同样的问题("float"对象没有属性"split"(,这就是我管理它的方式:
df['column_name'].astype(str(