18

Here I got many list with different length, like a=[1,2,3] and b=[2,3]

I would like to generate a pd.DataFrame from them, by padding nan at the end of list, like this:

   a  b
1  1  2 
2  2  3
3  3  nan

Any good idea to help me do so?

Garvey
  • 1,105
  • 2
  • 12
  • 25

1 Answers1

35

Use

In [9]: pd.DataFrame({'a': pd.Series(a), 'b': pd.Series(b)})
Out[9]:
   a    b
0  1  2.0
1  2  3.0
2  3  NaN

Or,

In [10]: pd.DataFrame.from_dict({'a': a, 'b': b}, orient='index').T
Out[10]:
     a    b
0  1.0  2.0
1  2.0  3.0
2  3.0  NaN
Zero
  • 66,763
  • 15
  • 141
  • 151