Creating dataframe from a dictionary where entries have different lengths

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

    You can also use pd.concat along axis=1 with a list of pd.Series objects:

    import pandas as pd, numpy as np
    
    d = {'A': np.array([1,2]), 'B': np.array([1,2,3,4])}
    
    res = pd.concat([pd.Series(v, name=k) for k, v in d.items()], axis=1)
    
    print(res)
    
         A  B
    0  1.0  1
    1  2.0  2
    2  NaN  3
    3  NaN  4
    

提交回复
热议问题