How do I Pass a List of Series to a Pandas DataFrame?

后端 未结 6 1998
栀梦
栀梦 2021-02-03 23:57

I realize Dataframe takes a map of {\'series_name\':Series(data, index)}. However, it automatically sorts that map even if the map is an OrderedDict().

Is there a simpl

6条回答
  •  孤城傲影
    2021-02-04 00:49

    a = pd.Series(data=[1,2,3])
    b = pd.Series(data=[4,5,6])
    a.name = 'a'
    b.name= 'b'
    
    pd.DataFrame(zip(a,b), columns=[a.name, b.name])
    

    or just concat dataframes

    pd.concat([pd.DataFrame(a),pd.DataFrame(b)], axis=1)
    
    In [53]: %timeit pd.DataFrame(zip(a,b), columns=[a.name, b.name])
    1000 loops, best of 3: 362 us per loop
    
    In [54]: %timeit pd.concat([pd.DataFrame(a),pd.DataFrame(b)], axis=1)
    1000 loops, best of 3: 808 us per loop
    

提交回复
热议问题