Python 创建数据帧的问题
我正在尝试使用for循环创建一个数据帧。它可以工作,但数据帧的输出不正确。数据帧的每个单元格都包含所有数据。我可以知道怎么修理吗 代码如下:Python 创建数据帧的问题,python,pandas,dataframe,Python,Pandas,Dataframe,我正在尝试使用for循环创建一个数据帧。它可以工作,但数据帧的输出不正确。数据帧的每个单元格都包含所有数据。我可以知道怎么修理吗 代码如下: from pandas_datareader import data import datetime from math import exp, sqrt import pandas as pd records = [] test = ['AAPL','AAL'] for i in test: stock_price = data.Dat
from pandas_datareader import data
import datetime
from math import exp, sqrt
import pandas as pd
records = []
test = ['AAPL','AAL']
for i in test:
stock_price = data.DataReader(test,
start='2021-01-01',
end='2021-04-01',
data_source='yahoo')['Adj Close'][-100:]
stock_volume = data.DataReader(test,
start='2021-01-01',
end='2021-04-01',
data_source='yahoo')['Volume'][-100:]
returns = stock_price.pct_change()
((1 + returns).cumprod() - 1)
records.append({
'underlyingSymbol' : i,
'last_price' : stock_price.iloc[-1],
'15d_highest' : stock_price.iloc[-15:].max(),
'15d_lowest' : stock_price.iloc[-15:].min(),
})
df = pd.DataFrame(records)
df
由于在符号上循环,您应该将
data.DataReader(test…
更改为data.DataReader(i…
)(否则,它会在每次迭代中读取这两个符号的数据):
输出:
underlyingSymbol last_price 15d_highest 15d_lowest 15d_volume \
0 AAPL 123.000000 125.57 119.900002 92403800.0
1 AAL 23.860001 25.17 21.809999 93746800.0
30d_returns 15d_returns 7d_returns volatility
0 -0.047342 0.018057 0.024240 0.325800
1 0.266432 0.030475 0.092192 0.571564
当然,不客气!如果有帮助,你能接受答案吗?谢谢!
underlyingSymbol last_price 15d_highest 15d_lowest 15d_volume \
0 AAPL 123.000000 125.57 119.900002 92403800.0
1 AAL 23.860001 25.17 21.809999 93746800.0
30d_returns 15d_returns 7d_returns volatility
0 -0.047342 0.018057 0.024240 0.325800
1 0.266432 0.030475 0.092192 0.571564