quickest way to to convert list of tuples to a series

前端 未结 4 1583
误落风尘
误落风尘 2021-02-19 12:59

Consider a list of tuples lst

lst = [(\'a\', 10), (\'b\', 20)]

question
What is the quickest way to

4条回答
  •  花落未央
    2021-02-19 13:42

    One approach with NumPy assuming regular length list -

    arr = np.asarray(lst)
    out = pd.Series(arr[:,1], index = arr[:,0])
    

    Sample run -

    In [147]: lst = [('a', 10), ('b', 20), ('j',1000)]
    
    In [148]: arr = np.asarray(lst)
    
    In [149]: pd.Series(arr[:,1], index = arr[:,0])
    Out[149]: 
    a      10
    b      20
    j    1000
    dtype: object
    

提交回复
热议问题