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 simple way to pass a list of Series(data, index, name=name) such that the order is preserved and the column names are the series.name? Is there an easy way if all the indices are the same for all the series?
I normally do this by just passing a numpy column_stack of series.values and specifying the column names. However, this is ugly and in this particular case the data is strings not floats.
Advertisement
Answer
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