首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Python的特定列中向df添加列表?熊猫相关

如何在Python的特定列中向df添加列表?熊猫相关
EN

Stack Overflow用户
提问于 2022-03-12 01:38:04
回答 2查看 33关注 0票数 1

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

代码语言:javascript
复制
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的列表

代码语言:javascript
复制
 volatility_list = [
                   ['Low Volatility Expected'],
                   ['Low Volatility Expected'],
                   ['High Volatility Expected'],
                   ['High Volatility Expected'],
                   ]

我如何从volatility_list列中向Volatility expected列添加df值,使其以这样的方式结束?

代码语言:javascript
复制
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%
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-12 01:44:20

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

代码语言:javascript
复制
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%
票数 1
EN

Stack Overflow用户

发布于 2022-03-12 01:45:41

你可以分配它和explode

代码语言:javascript
复制
df['Volatility expected'] = volatility_list
df = df.explode('Volatility expected')

输出:

代码语言:javascript
复制
   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%  
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71446336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档