代码之家  ›  专栏  ›  技术社区  ›  NoahVerner

如何将列表列表添加到Python上特定列的df中?熊猫相关

  •  0
  • NoahVerner  · 技术社区  · 3 年前

    假设我有以下几点 df Volatility expected 专栏:

    Index Time   Currency   Volatility expected Event                                 Actual    Forecast    Previous
    0     02:00    GBP                          U.K. Construction Output (YoY) (Jan)  9.9%      9.2%        7.4%
    1     02:00    GBP                          Construction Output (MoM) (Jan)       1.1%      0.5%        2.0%
    2     02:00    GBP                          GDP (MoM)                             0.8%      0.2%       -0.2%
    3     02:00    GBP                          GDP (YoY)                             10.0%     9.3%        6.0%
                                                                                                     
    

    下面的列表称为 volatility_list :

     volatility_list = [
                       ['Low Volatility Expected'],
                       ['Low Volatility Expected'],
                       ['High Volatility Expected'],
                       ['High Volatility Expected'],
                       ]
    

    我该怎么补充呢 易变性列表 价值观 预期波动性 来自 df 结果是这样的?

    Index Time   Currency   Volatility expected      Event                                Actual Forecast  Previous
    0     02:00    GBP      Low Volatility Expected  U.K. Construction Output (YoY) (Jan)  9.9%     9.2%     7.4%
    1     02:00    GBP      Low Volatility Expected  Construction Output (MoM) (Jan)       1.1%     0.5%     2.0%
    2     02:00    GBP      High Volatility Expected GDP (MoM)                             0.8%     0.2%    -0.2%
    3     02:00    GBP      High Volatility Expected GDP (YoY)                             10.0%    9.3%     6.0%
    
    2 回复  |  直到 3 年前
        1
  •  1
  •   Corralien    3 年前

    您可以使用理解来提取列表中每个项目的第一个也是唯一一个元素:

    df['Volatility expected'] = [v[0] for v in volatility_list]
    print(df)
    
    # Output
        Time Currency       Volatility expected                                 Event Actual Forecast Previous
    0  02:00      GBP   Low Volatility Expected  U.K. Construction Output (YoY) (Jan)   9.9%     9.2%     7.4%
    1  02:00      GBP   Low Volatility Expected       Construction Output (MoM) (Jan)   1.1%     0.5%     2.0%
    2  02:00      GBP  High Volatility Expected                             GDP (MoM)   0.8%     0.2%    -0.2%
    3  02:00      GBP  High Volatility Expected                             GDP (YoY)  10.0%     9.3%     6.0%
    
        2
  •  1
  •   enke    3 年前

    你可以分配它,然后 explode :

    df['Volatility expected'] = volatility_list
    df = df.explode('Volatility expected')
    

    输出:

       Index   Time Currency       Volatility expected                                 Event Actual Forecast Previous  
    0      0  02:00      GBP   Low Volatility Expected  U.K. Construction Output (YoY) (Jan)   9.9%     9.2%     7.4%  
    1      1  02:00      GBP   Low Volatility Expected       Construction Output (MoM) (Jan)   1.1%     0.5%     2.0%  
    2      2  02:00      GBP  High Volatility Expected                             GDP (MoM)   0.8%     0.2%    -0.2%  
    3      3  02:00      GBP  High Volatility Expected                             GDP (YoY)  10.0%     9.3%     6.0%