Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

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.

like image 847
rhaskett Avatar asked Nov 30 '12 20:11

rhaskett


People also ask

How do you create a DataFrame from a series list?

You can create a DataFrame from multiple Series objects by adding each series as a columns. By using concat() method you can merge multiple series together into DataFrame. This takes several params, for our scenario we use list that takes series to combine and axis=1 to specify merge series as columns instead of rows.

How do I get my series list on pandas?

Pandas series can be converted to a list using tolist() or type casting method. There can be situations when you want to perform operations on a list instead of a pandas object. In such cases, you can store the DataFrame columns in a list and perform the required operations.

Can you append series to DataFrame?

Appending a multiple rows - Appending a list of Dictionaries to a DataFrame. You can also pass a list of Series or a list of Dictionaries to append multiple rows.


1 Answers

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 
like image 128
unutbu Avatar answered Sep 28 '22 06:09

unutbu