假設我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%
uj5u.com熱心網友回復:
您可以使用推導來提取串列中每個專案的第一個也是唯一一個元素:
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%
uj5u.com熱心網友回復:
您可以分配它并且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://www.uj5u.com/gongcheng/441193.html
標籤:python-3.x 熊猫 列表 数据框 附加
上一篇:如何在一個回圈后將值保存在行中?
下一篇:從IP地址范圍獲取城市
