将pandas dataframe转换为utf8

ADI*_*MAR 7 python-3.x pandas

如何将pandas数据帧转换为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))`
Run Code Online (Sandbox Code Playgroud)

它给出了错误

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
Run Code Online (Sandbox Code Playgroud)

san*_*epp 2

更改代码

messages.head().apply(split_into_tokens(messages))
Run Code Online (Sandbox Code Playgroud)

messages.head().apply(split_into_tokens)
Run Code Online (Sandbox Code Playgroud)

当使用“apply”与像您的情况一样的函数时,不需要传递参数,因为您的代码显示它正在传递一个在执行时出错的数据帧。