How Do I Pass A List Of Series To A Pandas Dataframe?
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
Solution 1:
You could use pandas.concat
:
import pandas as pd
from pandas.util.testing import rands
data = [pd.Series([rands(4) for j inrange(6)],
index=pd.date_range('1/1/2000', periods=6),
name='col'+str(i)) for i inrange(4)]
df = pd.concat(data, axis=1, keys=[s.name for s in data])
print(df)
yields
col0col1col2col32000-01-01 GqcNLwljKm7bXfaA2000-01-02 lhNCnlSmjCYuXLVb2000-01-03 sSRzPFbyC1o50BJe2000-01-04 khZbNy9pcrUYLNmc2000-01-05 hmLp4rVpxF2POmD92000-01-06 giahpsQbT5RJoLSh
Solution 2:
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]:%timeitpd.DataFrame(zip(a,b),columns=[a.name,b.name])1000 loops,best of 3:362usperloopIn [54]:%timeitpd.concat([pd.DataFrame(a),pd.DataFrame(b)],axis=1)1000 loops,best of 3:808usperloop
Solution 3:
Build the list of series:
import pandas as pd
import numpy as np
> series = [pd.Series(np.random.rand(3), name=c) for c inlist('abcdefg')]
First methodpd.DataFrame.from_items
:
> pd.DataFrame.from_items([(s.name, s) for s in series])
ab c d e f g
00.0710940.0775450.2995400.3775550.7518400.8799950.93339910.5382510.0667800.4156070.7960590.7188930.6799500.50213820.0960010.6808680.8837780.2104880.6425780.0238810.250317
Second methodpd.concat
:
> pd.concat(series, axis=1)
abcdefg00.0710940.0775450.2995400.3775550.7518400.8799950.93339910.5382510.0667800.4156070.7960590.7188930.6799500.50213820.0960010.6808680.8837780.2104880.6425780.0238810.250317
Solution 4:
Simply passing the list of Series to DataFrame
then transposing seems to work too. It will also fill in any indices that are missing from one or the other Series.
import pandas as pd
from pandas.util.testing import rands
data = [pd.Series([rands(4) for j inrange(6)],
index=pd.date_range('1/1/2000', periods=6),
name='col'+str(i)) for i inrange(4)]
df = pd.DataFrame(data).T
print(df)
Solution 5:
Check out DataFrame.from_items
too
Post a Comment for "How Do I Pass A List Of Series To A Pandas Dataframe?"