将 pandas 数据框转换为utf8 [英] convert pandas dataframe to utf8
本文介绍了将 pandas 数据框转换为utf8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将pandas数据框转换为unicode? p>
How to convert pandas dataframe to unicode?
`messages=pandas.read_csv('data/SMSSpamCollection',sep='\t',quoting=csv.QUOTE_NONE,names=["label", "message"])
def split_into_tokens(message):
message = unicode(message, 'utf8') # convert bytes into proper unicode
return TextBlob(message).words
messages.head().apply(split_into_tokens(messages))`
出现错误
Traceback (most recent call last):
File "minor.py", line 46, in <module>
messages.head().apply(split_into_tokens(messages))
File "minor.py", line 42, in split_into_tokens
message = unicode(message, 'utf8') # convert bytes into proper unicode
TypeError: coercing to Unicode: need string or buffer, DataFrame found
推荐答案
更改代码
messages.head().apply(split_into_tokens(messages))
到
messages.head().apply(split_into_tokens)
在将"apply"与功能结合使用时(例如您的情况)不需要传递参数,因为您的代码表明它正在传递一个数据帧,该数据帧在执行时产生错误.
while using 'apply' with a funtion like in your case passing parameters is not required, as your code shows it is passing a dataframe which is giving error on execution.
这篇关于将 pandas 数据框转换为utf8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文