pandas ,将unicode的列转换为字符串列表的列 [英] Pandas, convert column of unicodes to column of list of strings
问题描述
我的pandas数据框列之一具有这种u'asd,abc,tre,der34,whatever'
的Unicode.最终结果应该是一列字符串列表:['asd','abc','tre','der34','whatever']
. Unicode列表也可以:[u'asd',u'abc',u'tre',u'der34',u'whatever']
.
One of my pandas dataframe columns has unicodes of this kind u'asd,abc,tre,der34,whatever'
. The final results should be a column of lists of strings: ['asd','abc','tre','der34','whatever']
. A list of unicodes might do, too: [u'asd',u'abc',u'tre',u'der34',u'whatever']
.
顺便说一句,tt可能会在unicodes列中出现nan或u''.
By the way, tt can happen that in the column of unicodes there is a nan or a u''.
有什么建议吗?我知道我可以做str(df['column'].iloc[0]).split(',')
并手动添加新列或做一些更棘手的事情,但是我一直在寻找一些更Python的东西.
Any suggestion? I know I can do str(df['column'].iloc[0]).split(',')
and manually add a new column or do something trickier, but I was looking for something more pythonic.
推荐答案
此解决方案似乎有效:
df['Column'] =df['Column'].astype(str).str.split(',')
这篇关于 pandas ,将unicode的列转换为字符串列表的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!