【问题标题】:Convert a Pandas Core Series with Dictionary in Rows into Pandas Dataframe将带有字典的 Pandas 核心系列转换为 Pandas 数据框
【发布时间】:2020-09-08 23:44:08
【问题描述】:

我有一个名为“series_1”的 Pandas Core 系列,每行包含三个变量(“V-Ratio”、“Z-score”、“P”)的字典。它看起来像这样:

V1    [{'V-Ratio': 0.98, 'Z-score': -0.94, 'P': 0.17}]
V2    [{'V-Ratio': 0.97, 'Z-score': -1.34, 'P': 0.09}]
V3     [{'V-Ratio': 1.03, 'Z-score': 1.21, 'P': 0.89}]
V4     [{'V-Ratio': 1.04, 'Z-score': 1.41, 'P': 0.92}]
V5    [{'V-Ratio': 0.99, 'Z-score': -0.42, 'P': 0.34}]
V6    [{'V-Ratio': 0.96, 'Z-score': -1.72, 'P': 0.04}]
V7      [{'V-Ratio': 1.02, 'Z-score': 0.7, 'P': 0.76}]
V8    [{'V-Ratio': 0.97, 'Z-score': -1.09, 'P': 0.14}]
V9     [{'V-Ratio': 0.98, 'Z-score': -0.7, 'P': 0.24}]
V10     [{'V-Ratio': 0.76, 'Z-score': -9.31, 'P': 0.0}]
V11     [{'V-Ratio': 0.83, 'Z-score': -6.74, 'P': 0.0}]
V12    [{'V-Ratio': 0.92, 'Z-score': -3.14, 'P': 0.0}]
V13     [{'V-Ratio': 0.92, 'Z-score': -3.23, 'P': 0.0}]
V14     [{'V-Ratio': 0.87, 'Z-score': -4.99, 'P': 0.0}]
V15     [{'V-Ratio': 1.02, 'Z-score': 0.75, 'P': 0.77}]
V16       [{'V-Ratio': 0.9, 'Z-score': -3.7, 'P': 0.0}]
V17     [{'V-Ratio': 0.95, 'Z-score': -2.1, 'P': 0.02}]

我想将其转换为如下所示的 Pandas Dataframe:

    V-Ratio Z-score  P
V1  0.98   -0.94    0.17
V2  0.97    1.34    0.09
V3  1.03    1.21    0.89
V4  1.04    1.41    0.92
V5  0.99   -0.42    0.34
.........................
.........................
.........................

我尝试过的:

  1. "series = pd.DataFrame(series_1)" 给了我一个数据框,其中有一列的字典包含行中的变量。
  2. “series = pd.DataFrame([series_1])”没有达到我的结果。
  3. “series = pd.DataFrame(series_1, index=[0])”不起作用。

有人知道如何解决我的问题吗?

【问题讨论】:

    标签: python pandas dataframe series


    【解决方案1】:

    请检查

    column 表示您拥有字典列表的列名。

    df = df[column].apply(pd.Series)[0].apply(pd.Series)
    

    输出

        V-Ratio Z-score P
    V1  0.98    -0.94   0.17
    V2  0.97    -1.34   0.09
    V3  1.03    1.21    0.89
    V4  1.04    1.41    0.92
    V5  0.99    -0.42   0.34
    

    【讨论】:

      【解决方案2】:

      我认为主要问题是,V1、V2、...中的每一个都是一个字典列表,但是每个列表中只有一个元素。 理想情况下,你会想要类似

      [{'V-Ratio': 0.98, 'Z-score': -0.94, 'P': 0.17}, {'V-Ratio': 0.97, 'Z-score': -1.34, 'P': 0.09}, ....],每个元素对应V1V2等,但你有[[{'V-Ratio': 0.98, 'Z-score': -0.94, 'P': 0.17}], [{'V-Ratio': 0.97, 'Z-score': -1.34, 'P': 0.09}], ....]请检查一下。 之后,您可以尝试您所做的步骤,或者使用.tolist() 将系列转换为列表并转换为数据框。

      如果您无法更改,则只需将每个系列列表的第一个元素转换为数据框

      【讨论】:

        【解决方案3】:

        试试这个你需要先从list获得dict

        # here, `s` is your series
        df = s.str[0].apply(pd.Series)      
        df
            V-Ratio  Z-score     P
        v1     0.98    -0.94  0.17
        v2     0.97    -1.34  0.09
        v3     1.03     1.21  0.89
        ......
        

        df = s.apply(lambda x: pd.Series(x[0]))
        

        【讨论】:

          猜你喜欢
          • 2014-06-12
          • 1970-01-01
          • 2019-02-22
          • 2017-07-16
          • 2020-12-22
          • 1970-01-01
          • 1970-01-01
          • 2018-09-12
          相关资源
          最近更新 更多