Pandas,将unicodes列转换为字符串列表列

2024-09-29 20:16:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我的一个pandas dataframe列有这种类型的unicodeu'asd,abc,tre,der34,whatever'。最后的结果应该是一列字符串:['asd','abc','tre','der34','whatever']。独角兽的列表也可以:[u'asd',u'abc',u'tre',u'der34',u'whatever']

顺便说一句,tt可以发生在unicodes列中有一个nan或一个u''。

有什么建议吗?我知道我可以做str(df['column'].iloc[0]).split(',')和手动添加一个新的列或做一些更棘手的事情,但我正在寻找更Python。


Tags: 字符串类型dataframepandas列表nantreabc
2条回答

这个解决方案似乎有效:

df['Column'] =df['Column'].astype(str).str.split(',')

这应该是可行的,如果有nan或空字符串,您将不得不处理,无论您认为合适。

In [1]: [str(col) for col in u'asd,abc,tre,der34,whatever'.split(',')]

Out[1]: ['asd', 'abc', 'tre', 'der34', 'whatever']

相关问题 更多 >

    热门问题