Split string in pandas dataframe into individual columns – no delimiter

Question:

I have a pandas dataframe df and one of the columns df['x'] contains strings of length 100, each string of the form 'ABBABBBABBAABAABABBAAABAAAA...'

How would I go about splitting this 1 column into 100 different columns, each containing only 1 character from the original string?

Asked By: Mike

||

Answers:

You can use str.split:

df['x'].str.split('',expand=True).iloc[:,1:-1]

or str.extractall with unstack:

df['x'].str.extractall('(.)')[0].unstack()
Answered By: Quang Hoang
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.