假设我在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'],
]我如何从volatility_list列中向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%发布于 2022-03-12 01:44:20
您可以使用理解来提取列表中每个项目的第一个也是唯一的元素:
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%发布于 2022-03-12 01:45:41
你可以分配它和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% https://stackoverflow.com/questions/71446336
复制相似问题