How to expand one column in Pandas to many columns?
Not as fast as @jezrael's solution. But elegant :-)
apply
with pd.Series
df.a.apply(pd.Series) 0 1 2 3 4 50 0 1 2 3 4 51 0 1 2 3 4 5
or
df.a.apply(pd.Series, index=list('abcdef')) a b c d e f0 0 1 2 3 4 51 0 1 2 3 4 5
You can convert lists to numpy array
by values
and then use DataFrame
constructor:
df = pd.DataFrame({'a':[[0,1,2,3,4,5],[0,1,2,3,4,5]]})print (df) a0 [0, 1, 2, 3, 4, 5]1 [0, 1, 2, 3, 4, 5]df1 = pd.DataFrame(df['a'].values.tolist())print (df1) 0 1 2 3 4 50 0 1 2 3 4 51 0 1 2 3 4 5
cols = list('abcdef')df1 = pd.DataFrame(df['a'].values.tolist(), columns=cols)print (df1) a b c d e f0 0 1 2 3 4 51 0 1 2 3 4 5
If I understood your question correctly, you are looking for a transpose operation.
df = pd.DataFrame([1,2,3,4,5],columns='a')# .T stands for transposeprint(df.T)