Creating dataframe from a dictionary where entries have different lengths

前端 未结 9 1682
生来不讨喜
生来不讨喜 2020-11-22 14:04

Say I have a dictionary with 10 key-value pairs. Each entry holds a numpy array. However, the length of the array is not the same for all of them.

How can I create a

9条回答
  •  耶瑟儿~
    2020-11-22 14:44

    While this does not directly answer the OP's question. I found this to be an excellent solution for my case when I had unequal arrays and I'd like to share:

    from pandas documentation

    In [31]: d = {'one' : Series([1., 2., 3.], index=['a', 'b', 'c']),
       ....:      'two' : Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
       ....: 
    
    In [32]: df = DataFrame(d)
    
    In [33]: df
    Out[33]: 
       one  two
    a    1    1
    b    2    2
    c    3    3
    d  NaN    4
    

提交回复
热议问题