0

My Dataframe is like:

       col1
   0   AGCT
   1   AGCT

How to turn it like this:

   col1 col2 col3 col4
0   A     G    C    T
1   A     G    C    T

2 Answers 2

3

Option 1
list comprehension - You'd be surprised at how fast this is.

pd.DataFrame([list(x) for x in df.col1])

   0  1  2  3
0  A  G  C  T
1  A  G  C  T

Option 2
pd.Series.apply (less performant)

pd.DataFrame(df.col1.apply(list).tolist())

   0  1  2  3
0  A  G  C  T
1  A  G  C  T

Option 3
pd.Series.extractall + unstack (least performant)

df.col1.str.extractall('(.)')[0].unstack()

match  0  1  2  3
0      A  G  C  T
1      A  G  C  T
Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain what .apply(list) does? list here is a function?
@Spencer it applies a given function rowwise/columnwise for a dataframe. In this case, I called apply rowwise.
2

IIUC

df=df.col1.apply(list).apply(pd.Series)
df
Out[645]: 
   0  1  2  3
0  A  G  C  T
1  A  G  C  T

If performance is matter :-)

pd.DataFrame(list(map(list,df.col1.values)))
Out[647]: 
   0  1  2  3
0  A  G  C  T
1  A  G  C  T

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.