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

后端 未结 6 2002
栀梦
栀梦 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:38

    Build the list of series:

    import pandas as pd
    import numpy as np
    
    > series = [pd.Series(np.random.rand(3), name=c) for c in list('abcdefg')]
    

    First method pd.DataFrame.from_items:

    > pd.DataFrame.from_items([(s.name, s) for s in series])
              a         b         c         d         e         f         g
    0  0.071094  0.077545  0.299540  0.377555  0.751840  0.879995  0.933399
    1  0.538251  0.066780  0.415607  0.796059  0.718893  0.679950  0.502138
    2  0.096001  0.680868  0.883778  0.210488  0.642578  0.023881  0.250317
    

    Second method pd.concat:

    > pd.concat(series, axis=1)
              a         b         c         d         e         f         g
    0  0.071094  0.077545  0.299540  0.377555  0.751840  0.879995  0.933399
    1  0.538251  0.066780  0.415607  0.796059  0.718893  0.679950  0.502138
    2  0.096001  0.680868  0.883778  0.210488  0.642578  0.023881  0.250317
    

提交回复
热议问题