如何解决属性错误';浮动';对象没有属性';拆分';用python?
当我运行下面的代码时,它给出了一个错误,表示存在属性错误:“float”对象在python中没有属性“split” 我想知道为什么会出现这种错误如何解决属性错误';浮动';对象没有属性';拆分';用python?,python,string,pandas,series,attributeerror,Python,String,Pandas,Series,Attributeerror,当我运行下面的代码时,它给出了一个错误,表示存在属性错误:“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
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 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'
回溯(最近一次呼叫最后一次):
文件“C:\Program Files\JetBrains\PyCharm Community Edition 2018.2.2\helpers\pydev\pydevd.py”,第1664行,在
main()
文件“C:\Program Files\JetBrains\PyCharm Community Edition 2018.2.2\helpers\pydev\pydevd.py”,第1658行,主目录
globals=debugger.run(setup['file'],None,None,is_模块)
文件“C:\Program Files\JetBrains\PyCharm Community Edition 2018.2.2\helpers\pydev\pydevd.py”,第1068行,正在运行
pydev_imports.execfile(文件、全局、局部)#执行脚本
文件“C:\Program Files\JetBrains\PyCharm社区版2018.2.2\helpers\pydev\\u pydev\u imps\\u pydev\u execfile.py”,execfile中第18行
exec(编译(内容+“\n”,文件,'exec'),全局,loc)
文件“C:/Users/L31307/Documents/FYP P3_Lynn_161015H/FYP 10.10.18(Wed)仍在进行it/FYP/dataanalysis/category_analysis.py”,第53行
df=文本处理(df)
文件“C:/Users/L31307/Documents/FYP P3_Lynn_161015H/FYP 10.10.18(Wed)仍在进行it/FYP/dataanalysis/category_analysis.py”,第30行,文本处理
df['content']=df['content'].apply(lambda x::.join(x.lower()表示x中的x.split(),如果x不在stop_字中)
文件“C:\Users\L31307\AppData\Roaming\Python37\site packages\pandas\core\series.py”,第3194行,在apply中
mapped=lib.map\u推断(值,f,convert=convert\u数据类型)
文件“pandas/_libs/src\expression.pyx”,第1472行,在pandas._libs.lib.map\u expert中
文件“C:/Users/L31307/Documents/FYP P3_Lynn_161015 H/FYP 10.10.18(Wed)仍在进行it/FYP/dataanalysis/category_analysis.py”,第30行
df['content']=df['content'].apply(lambda x::.join(x.lower()表示x中的x.split(),如果x不在stop_字中)
AttributeError:“float”对象没有属性“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
/的命名函数,但
子句除外:
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()方法的浮点值
尝试使用str(x).split()将值转换为字符串,或者先将整个列转换为字符串,这样效率会更高。您可以按如下方式执行此操作:
df['column_name'].astype(str)
你一开始就明白为什么会出错吗?无论您有什么数据帧,它的值都是
float
type。但是您正在对它们调用字符串函数split
。你期望它如何工作?转换列或进行检查,以便split跳过浮点类型的列。错误消息表示列df['content']
包含float
s,不能split
。我收到了相同类型的错误,在删除空值或nan值后,使用df.dropna(inplace=True)
,上述错误已不存在。这将修复该问题。谢谢words.split(“”)改为str(words.split(“”)