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

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

    You could use pandas.concat:

    import pandas as pd
    from pandas.util.testing import rands
    
    data = [pd.Series([rands(4) for j in range(6)],
                      index=pd.date_range('1/1/2000', periods=6),
                      name='col'+str(i)) for i in range(4)]
    
    df = pd.concat(data, axis=1, keys=[s.name for s in data])
    print(df)
    

    yields

                col0  col1  col2  col3
    2000-01-01  GqcN  Lwlj  Km7b  XfaA
    2000-01-02  lhNC  nlSm  jCYu  XLVb
    2000-01-03  sSRz  PFby  C1o5  0BJe
    2000-01-04  khZb  Ny9p  crUY  LNmc
    2000-01-05  hmLp  4rVp  xF2P  OmD9
    2000-01-06  giah  psQb  T5RJ  oLSh
    

提交回复
热议问题