Py学习  »  Python

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

NoahVerner • 3 年前 • 1342 次点击  

假设我有以下几点 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%
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/128543
 
1342 次点击  
文章 [ 2 ]  |  最新文章 3 年前
enke
Reply   •   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%  
Corralien
Reply   •   2 楼
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%