Pandas, convert column of unicodes to column of list of strings
21,386
This solution seems to work:
df['Column'] =df['Column'].astype(str).str.split(',')
Author by
foebu
Physicist, data scientist, pythonista. I built and work on a personal fantasy football managerial web app as a personal project, Django is its skeleton.
Updated on December 22, 2020Comments
-
foebu over 3 years
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']
.By the way, tt can happen that in the column of unicodes there is a nan or a u''.
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.