Pandas:将 unicode 字符串转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38786936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:44:11 来源:igfitidea点击:
Pandas: convert unicode strings to string
提问by ldevyataykina
I have data frame and there are 2 columns with unicode value. I need to convert it to string. I try df.domain.astype(str)
but it return unicode strings.
How can I do that?
Data looks like (I need to convert either columns)
我有数据框,有 2 列具有 unicode 值。我需要将其转换为字符串。我尝试df.domain.astype(str)
但它返回 unicode 字符串。我怎样才能做到这一点?数据看起来像(我需要转换任一列)
domain search_term
vk.com None
facebook.com None
yandex.ru снять квартиру
locals.ru None
yandex.ru снять квартиру без посредников в Москве
avito.ru None
回答by SerialDev
Would this help?:
这有帮助吗?:
for col in types[types=='unicode'].index:
df[col] = df[col].astype(str)
or:
或者:
for col in types[types=='unicode'].index:
df[col] = df[col].apply(lambda x: x.encode('utf-8').strip())