Search code examples
python-3.xpandasdataframestatsmodels

Converting a List of Pandas Series to a single Pandas DataFrame


I am using statsmodels.api on my data set. I have a list of panda series. The panda series has key value pairs. The keys are the names of the columns and the values contain the data. But, I have a list of series where the keys (column names) are repeated. I want to save all of the values from the list of pandas series to a single dataframe where the column names are the keys of the panda series. All of the series in the list have the same keys. I want to save them as a single data frame so that I can export the dataframe as a CSV. Any idea how I can save the keys as my column names of the df and then have the values fill the rest of the information.

Each series in the list returns something like this:

index 0 of the list: <class 'pandas.core.series.Series'>

height     23
weight     10
size       45
amount      9 

index 1 of the list: <class 'pandas.core.series.Series'>

height     11
weight     99
size       25
amount     410 

index 2 of the list: <class 'pandas.core.series.Series'>

height     3
weight     0
size       115
amount     92 

I would like to be able to read a dataframe such that these values are saved as the following:

DataFrame:

height   weight   size   amount
  23       10      45      9
  11       11      25      410
   3        3      115     92

Solution

  • pd.DataFrame(data=your_list_of_series)
    

    When creating a new DataFrame, pandas will accept a list of series for the data argument. The indices of your series will become the column names of the DataFrame.