【问题标题】:Converting a List of Pandas Series to a single Pandas DataFrame将 Pandas 系列列表转换为单个 Pandas DataFrame
【发布时间】:2019-11-28 17:58:07
【问题描述】:

我在我的数据集上使用statsmodels.api。我有一个熊猫系列的清单。熊猫系列有键值对。键是列的名称,值包含数据。但是,我有一个重复键(列名)的系列列表。我想将熊猫系列列表中的所有值保存到单个数据框中,其中列名是熊猫系列的键。列表中的所有系列都具有相同的键。我想将它们保存为单个数据框,以便可以将数据框导出为 CSV。知道如何将键保存为 df 的列名,然后让值填充其余信息。

列表中的每个系列都返回如下内容:

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 

我希望能够读取数据框,以便将这些值保存为以下内容:

DataFrame:

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

【问题讨论】:

  • 检查 pd.concat(l)

标签: python-3.x pandas dataframe statsmodels


【解决方案1】:
pd.DataFrame(data=your_list_of_series)

当创建一个新的 DataFrame 时,pandas 将接受一个系列列表作为 data 参数。您的系列的索引将成为 DataFrame 的列名。

【讨论】:

    【解决方案2】:

    不是最有效的方法,但可以解决问题:

    import pandas as pd
    
    series_list =[  pd.Series({ 'height':     23,
                            'weight':     10,
                            'size':       45,
                            'amount':      9
                          }),
                pd.Series({ 'height':     11,
                            'weight':     99,
                            'size':       25,
                            'amount':      410
                         }),
    
                pd.Series({ 'height':     3,
                            'weight':     0,
                            'size':       115,
                            'amount':      92
                         })
            ]
    
    pd.DataFrame( [series.to_dict() for series in series_list] )
    

    【讨论】:

    • 这不能解决创建的重复键。我只需要一组带有值列表的键,就像 excel 文件的外观一样。
    • 我的解决方案产生了独特的指标。 DataFrame 没有键,但类比是索引。
    • 它也与我之后发布的 2 个相同,但接受的一个是一般性的,而不是回答您的具体情况,我想这很酷
    【解决方案3】:

    您是否尝试在系列列表中仅调用pd.DataFrame()?那应该行得通。

    import pandas as pd
    
    series_list = [
        pd.Series({
                'height': 23,
                'weight': 10,
                'size': 45,
                'amount': 9
            }),
            pd.Series({
                'height': 11,
                'weight': 99,
                'size': 25,
                'amount': 410
            }),
            pd.Series({
                'height': 3,
                'weight': 0,
                'size': 115,
                'amount': 92
            })
        ]
    df = pd.DataFrame(series_list)
    print(df)
    df.to_csv('path/to/save/foo.csv')
    

    输出:

       height  weight  size  amount
    0      23      10    45       9
    1      11      99    25     410
    2       3       0   115      92
    

    【讨论】:

      猜你喜欢
      • 2015-08-08
      • 2021-03-01
      • 2019-10-27
      • 2018-02-02
      • 2015-03-23
      • 2013-11-04
      • 1970-01-01
      • 2014-01-05
      相关资源
      最近更新 更多