從 Yahoo Finance (yfinance) 匯入資料后,我在從系列中洗掉美元符號時遇到問題。有誰知道我做錯了什么?這是我的代碼。
import yfinance as yf
start_date = '2016-01-01'
yf_eth_df = yf.download('ETH-USD',start_date, today)
yf_eth_df = yf_eth_df.reset_index()
yf_eth_df.tail()
我創建了一個自定義列來獲取打開值和最高值之間的百分比變化。
輸出是:
Date Open High Low Close Adj Close Volume variation_open_high
0 2017-11-09 $308.64 $329.45 $307.06 $320.88 $320.88 893249984 $6.32
1 2017-11-10 $320.67 $324.72 $294.54 $299.25 $299.25 885985984 $1.25
2 2017-11-11 $298.59 $319.45 $298.19 $314.68 $314.68 842300992 $6.53
3 2017-11-12 $314.69 $319.15 $298.51 $307.91 $307.91 1613479936 $1.40
4 2017-11-13 $307.02 $328.42 $307.02 $316.72 $316.72 1041889984 $6.51
出于某種原因,正在使用$符號創建資料框。我想使用它從我的最后一列中洗掉它,yf_eth_df['variation_open_high'] = yf_eth_df['variation_open_high'].replace("$", '')但它不起作用。
uj5u.com熱心網友回復:
Series.replace僅替換完全匹配的值。要替換子字串,請使用Series.str.replace
yf_eth_df['variation_open_high'] = yf_eth_df['variation_open_high'].str.replace("$", '', regex=False)
請注意,洗掉美元符號后,值仍然是字串。似乎您希望它們作為浮點數,因此您必須顯式轉換它們
yf_eth_df['variation_open_high'] = (
yf_eth_df['variation_open_high'].str.replace("$", '', regex=False)
.astype(float)
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/451740.html
上一篇:barx-tick與圖片不一樣
