pandas 将对象列转换为str-列包含unicode,float等 [英] Pandas convert object column to str - column contains unicode, float etc

查看:143
本文介绍了 pandas 将对象列转换为str-列包含unicode,float等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有pandas数据框,其中列类型显示为object,但是当我尝试转换为字符串时,

I have pandas data frame where column type shows as object but when I try to convert to string,

df['column'] = df['column'].astype('str')

UnicodeEncodeError被抛出: *** UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)

我的下一个方法是处理编码部分: df['column'] = filtered_df['column'].apply(lambda x: x.encode('utf-8').strip())

My next approach was to handle the encoding part: df['column'] = filtered_df['column'].apply(lambda x: x.encode('utf-8').strip())

但是会出现以下错误: *** AttributeError: 'float' object has no attribute 'encode'

But that gives following error: *** AttributeError: 'float' object has no attribute 'encode'

将此列转换为字符串的最佳方法是什么.

Whats the best approach to convert this column to string.

列中的字符串示例

Thank you :)
Thank You !!!
responsibilities/assigned job.

推荐答案

当我尝试运行最初打算用于python 3的脚本时,在python 2.7中遇到了相同的问题.在python 2.7中,默认的str功能是编码为ASCII,这显然不适用于您的数据.可以在一个简单的示例中复制它:

I had the same problem in python 2.7 when trying to run a script that was originally intended for python 3. In python 2.7, the default str functionality is to encode to ASCII, which will apparently not work with your data. This can be replicated in a simple example:

import pandas as pd
df = pd.DataFrame({'column': ['asdf', u'uh ™ oh', 123]})
df['column'] = df['column'].astype('str')

结果:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2122' in position 3: ordinal not in range(128)

相反,您可以指定unicode:

Instead, you can specify unicode:

df['column'] = df['column'].astype('unicode')

验证数字已转换为字符串:

Verify that the number has been converted to a string:

df['column'][2]

这将输出u'123',因此已将其转换为unicode字符串.特殊字符™也已正确保存.

This outputs u'123', so it has been converted to a unicode string. The special character ™ has been properly preserved as well.

这篇关于 pandas 将对象列转换为str-列包含unicode,float等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆