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

How to add a list of lists to a df at an specific column on Python? Pandas related

假设我在 Volatility expected 列中有以下 df 和空字符串:

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'],
                   ]

如何将 volatility_list 值添加到 df 中的 Volatility expected 列,使其最终像这样?

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%

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

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%

您可以分配它并且 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%