如何更改数据框以便在 Python 中绘制数据
How to change a data frame in order to plot the data in Python
我想从 Yahoo finance 抓取多只股票,并将这些股票转换为欧元,例如,如果股票以美元或英镑计价。我有一个代码将历史股票转换为欧元,带有日期,但现在我必须制作 df 才能正确绘制数据。
这是我现在的代码。
import yfinance as yf
import pandas as pd
import numpy as np
start = '2013-01-01'
end = '2021-01-01'
rates = ['USDEUR=X', 'GBPEUR=X']
tickers = yf.Tickers(' '.join(rates))
exchange_rates = []
for i in range(len(tickers.tickers)):
exchange_rates.append(tickers.tickers[i].history(start=start, end=end).Close)
ex_df = pd.DataFrame(exchange_rates).T
ex_df.columns = rates
ex_df['EUREUR=X'] = 1.0
assets = {'^AEX':'EUR', 'AAPL':'USD', 'AZN.L':'GBP', 'AD.AS':'EUR', 'ASML.AS':'EUR'}
stock_df = pd.DataFrame()
for k,v in assets.items():
data = yf.download(k, start=start, end=end, progress=False).Close.to_frame()
data['ticker'] = k
if v[:3] == 'EUR':
data['rating'] = ex_df['EUREUR=X']
data['price'] = data['Close'] / data['rating']
elif v[:3] == 'USD':
data['rating'] = ex_df['USDEUR=X']
data['price'] = data['Close'] / data['rating']
elif v[:3] == 'GBP':
data['rating'] = ex_df['GBPEUR=X']
data['price'] = data['Close'] / data['rating']
else:
data['rating'] = np.NaN
data['price'] = np.NaN
stocks_df = pd.concat([stock_df, data], axis=0)
我会重新发布完整版的代码。
df.fillna()
两者相同。我还更换了图形图像。
祝你好运。
import yfinance as yf
import pandas as pd
import numpy as np
start = '2020-07-01'
end = '2021-01-01'
rates = ['USDEUR=X', 'GBPEUR=X']
tickers = yf.Tickers(' '.join(rates))
exchange_rates = []
for i in range(len(tickers.tickers)):
exchange_rates.append(tickers.tickers[i].history(start=start, end=end).Close)
ex_df = pd.DataFrame(exchange_rates).T
ex_df.columns = rates
ex_df['EUREUR=X'] = 1.0
assets = {'^AEX':'EUR', 'AAPL':'USD', 'AZN.L':'GBP', 'AD.AS':'EUR', 'ASML.AS':'EUR'}
stock_df = pd.DataFrame()
for k,v in assets.items():
data = yf.download(k, start=start, end=end, progress=False).Close.to_frame()
data['ticker'] = k
if v[:3] == 'EUR':
data['rating'] = ex_df['EUREUR=X']
data['price'] = data['Close'] / data['rating']
elif v[:3] == 'USD':
data['rating'] = ex_df['USDEUR=X']
data['price'] = data['Close'] / data['rating']
elif v[:3] == 'GBP':
data['rating'] = ex_df['GBPEUR=X']
data['price'] = data['Close'] / data['rating']
else:
data['rating'] = np.NaN
data['price'] = np.NaN
stock_df = pd.concat([stock_df, data], axis=0)
my_stocks = stock_df.pivot(columns='ticker', values='price')
my_stocks.head()
ticker AAPL AD.AS ASML.AS AZN.L ^AEX
Date
2020-06-30 102.574478 24.260000 326.899994 7694.486885 559.729980
2020-07-01 102.278087 23.879999 327.149994 7665.306388 562.669983
2020-07-02 102.418480 24.129999 332.750000 7758.488549 570.750000
2020-07-03 102.418480 23.870001 333.950012 7626.217088 568.630005
2020-07-06 105.119162 23.900000 343.549988 7810.244933 578.520020
import matplotlib.pyplot as plt
for c in my_stocks.columns.values[1:]:
plt.plot(my_stocks.index, my_stocks[c], label=c)
plt.title('title')
plt.xlabel('Date',fontsize=18)
plt.ylabel('Adj. Price € (EUR)',fontsize=18)
plt.legend(my_stocks.columns.values[1:], loc='center right')
plt.show()
我想从 Yahoo finance 抓取多只股票,并将这些股票转换为欧元,例如,如果股票以美元或英镑计价。我有一个代码将历史股票转换为欧元,带有日期,但现在我必须制作 df 才能正确绘制数据。 这是我现在的代码。
import yfinance as yf
import pandas as pd
import numpy as np
start = '2013-01-01'
end = '2021-01-01'
rates = ['USDEUR=X', 'GBPEUR=X']
tickers = yf.Tickers(' '.join(rates))
exchange_rates = []
for i in range(len(tickers.tickers)):
exchange_rates.append(tickers.tickers[i].history(start=start, end=end).Close)
ex_df = pd.DataFrame(exchange_rates).T
ex_df.columns = rates
ex_df['EUREUR=X'] = 1.0
assets = {'^AEX':'EUR', 'AAPL':'USD', 'AZN.L':'GBP', 'AD.AS':'EUR', 'ASML.AS':'EUR'}
stock_df = pd.DataFrame()
for k,v in assets.items():
data = yf.download(k, start=start, end=end, progress=False).Close.to_frame()
data['ticker'] = k
if v[:3] == 'EUR':
data['rating'] = ex_df['EUREUR=X']
data['price'] = data['Close'] / data['rating']
elif v[:3] == 'USD':
data['rating'] = ex_df['USDEUR=X']
data['price'] = data['Close'] / data['rating']
elif v[:3] == 'GBP':
data['rating'] = ex_df['GBPEUR=X']
data['price'] = data['Close'] / data['rating']
else:
data['rating'] = np.NaN
data['price'] = np.NaN
stocks_df = pd.concat([stock_df, data], axis=0)
我会重新发布完整版的代码。
df.fillna()
两者相同。我还更换了图形图像。
祝你好运。
import yfinance as yf
import pandas as pd
import numpy as np
start = '2020-07-01'
end = '2021-01-01'
rates = ['USDEUR=X', 'GBPEUR=X']
tickers = yf.Tickers(' '.join(rates))
exchange_rates = []
for i in range(len(tickers.tickers)):
exchange_rates.append(tickers.tickers[i].history(start=start, end=end).Close)
ex_df = pd.DataFrame(exchange_rates).T
ex_df.columns = rates
ex_df['EUREUR=X'] = 1.0
assets = {'^AEX':'EUR', 'AAPL':'USD', 'AZN.L':'GBP', 'AD.AS':'EUR', 'ASML.AS':'EUR'}
stock_df = pd.DataFrame()
for k,v in assets.items():
data = yf.download(k, start=start, end=end, progress=False).Close.to_frame()
data['ticker'] = k
if v[:3] == 'EUR':
data['rating'] = ex_df['EUREUR=X']
data['price'] = data['Close'] / data['rating']
elif v[:3] == 'USD':
data['rating'] = ex_df['USDEUR=X']
data['price'] = data['Close'] / data['rating']
elif v[:3] == 'GBP':
data['rating'] = ex_df['GBPEUR=X']
data['price'] = data['Close'] / data['rating']
else:
data['rating'] = np.NaN
data['price'] = np.NaN
stock_df = pd.concat([stock_df, data], axis=0)
my_stocks = stock_df.pivot(columns='ticker', values='price')
my_stocks.head()
ticker AAPL AD.AS ASML.AS AZN.L ^AEX
Date
2020-06-30 102.574478 24.260000 326.899994 7694.486885 559.729980
2020-07-01 102.278087 23.879999 327.149994 7665.306388 562.669983
2020-07-02 102.418480 24.129999 332.750000 7758.488549 570.750000
2020-07-03 102.418480 23.870001 333.950012 7626.217088 568.630005
2020-07-06 105.119162 23.900000 343.549988 7810.244933 578.520020
import matplotlib.pyplot as plt
for c in my_stocks.columns.values[1:]:
plt.plot(my_stocks.index, my_stocks[c], label=c)
plt.title('title')
plt.xlabel('Date',fontsize=18)
plt.ylabel('Adj. Price € (EUR)',fontsize=18)
plt.legend(my_stocks.columns.values[1:], loc='center right')
plt.show()