RData
RData

Reputation: 969

List to Pandas DataFrame - Python 3.x

All,

I have a list output as below and i would like to convert it to a DF.

attrb = [['nn', 'xx', 'yy'], ['aa', 'vv', 'bb'], ['cc', 'gg', 'hh'], ['jj', 'kk', 'uu'], ['ee', 'ww', 'qq'], ['tt', 'mm', 'ss'], ['tt', 'gg', 'll'], ['aa', 'qq', 'tt']]

as below

0    1    2  3   4   5   6   7  
nn   aa   cc jj  ee  tt  tt  aa     
xx   vv   gg  kk  ww  mm gg  qq
yy   bb   hh  uu  qq  ss  ll  tt

i have tried the below but this makes it a 3x8 matrix

 cf = pd.DataFrame(attrb)
 print(cf)

Upvotes: 2

Views: 138

Answers (1)

jezrael
jezrael

Reputation: 862781

The simpliest is transpose by T:

df = pd.DataFrame(attrb).T
print (df)
    0   1   2   3   4   5   6   7
0  nn  aa  cc  jj  ee  tt  tt  aa
1  xx  vv  gg  kk  ww  mm  gg  qq
2  yy  bb  hh  uu  qq  ss  ll  tt

Upvotes: 2

Related Questions