Creating dataframe from a dictionary where entries have different lengths

前端 未结 9 1755
生来不讨喜
生来不讨喜 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:27

    A way of tidying up your syntax, but still do essentially the same thing as these other answers, is below:

    >>> mydict = {'one': [1,2,3], 2: [4,5,6,7], 3: 8}
    
    >>> dict_df = pd.DataFrame({ key:pd.Series(value) for key, value in mydict.items() })
    
    >>> dict_df
    
       one  2    3
    0  1.0  4  8.0
    1  2.0  5  NaN
    2  3.0  6  NaN
    3  NaN  7  NaN
    

    A similar syntax exists for lists, too:

    >>> mylist = [ [1,2,3], [4,5], 6 ]
    
    >>> list_df = pd.DataFrame([ pd.Series(value) for value in mylist ])
    
    >>> list_df
    
         0    1    2
    0  1.0  2.0  3.0
    1  4.0  5.0  NaN
    2  6.0  NaN  NaN
    

    Another syntax for lists is:

    >>> mylist = [ [1,2,3], [4,5], 6 ]
    
    >>> list_df = pd.DataFrame({ i:pd.Series(value) for i, value in enumerate(mylist) })
    
    >>> list_df
    
       0    1    2
    0  1  4.0  6.0
    1  2  5.0  NaN
    2  3  NaN  NaN
    

    You may additionally have to transpose the result and/or change the column data types (float, integer, etc).

提交回复
热议问题