将字符串列中的字符分解为多个列

时间:2018-03-26 18:50:07

标签: python string pandas dataframe split

我的Dataframe就像:

       col1
   0   AGCT
   1   AGCT

如何把它变成这样:

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

2 个答案:

答案 0 :(得分:3)

选项1
list comprehension - 你会对这有多快感到惊讶。

-keyPathsForValuesAffectingPreview

选项2
pd.DataFrame([list(x) for x in df.col1]) 0 1 2 3 0 A G C T 1 A G C T (表现不佳)

pd.Series.apply

选项3
pd.DataFrame(df.col1.apply(list).tolist()) 0 1 2 3 0 A G C T 1 A G C T + pd.Series.extractall(性能最差)

unstack

答案 1 :(得分: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

如果表现很重要:-)

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