Creating dataframe from a dictionary where entries have different lengths

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

    Both the following lines work perfectly :

    pd.DataFrame.from_dict(df, orient='index').transpose() #A
    
    pd.DataFrame(dict([ (k,pd.Series(v)) for k,v in df.items() ])) #B (Better)
    

    But with %timeit on Jupyter, I've got a ratio of 4x speed for B vs A, which is quite impressive especially when working with a huge data set (mainly with a big number of columns/features).

提交回复
热议问题