我从互联网服务获得具有以下结构的熊猫数据框:
from yfinance module
DXCM
Open High Low Close Volume
Date
2020-01-30 237.850006 239.770004 232.000000 238.929993 459600
2020-01-31 238.820007 247.339996 238.009995 240.750000 1000100
2020-02-03 241.039993 243.839996 236.449997 237.089996 637700
2020-02-04 238.910004 245.259995 237.669998 243.699997 685500
SPY
Open High Low Close Volume
Date
2020-01-30 324.359985 327.910004 323.540009 327.679993 75491800
2020-01-31 327.000000 327.170013 320.730011 321.730011 113845600
2020-02-03 323.350006 326.160004 323.220001 324.119995 69242300
2020-02-04 328.070007 330.010010 327.720001 329.059998 62573200
DXCM和SPY按值分组=股票行情
我需要按值分组,即每行中都有SPY(=代码)。但是我找不到正确的代码。
我使用以下代码获取此数据框:
data = yf.download(ticker, start=von, end=bis,
group_by="ticker",interval= '1d', auto_adjust=True)
如果我按参数消除分组,则会使情况更糟。
请帮忙。
爱德华
最佳答案
试用以下代码:
import yfinance as yf
data = yf.download('DXCM SPY', start="2017-01-01", end="2017-04-30",
interval= '1d', auto_adjust=True)
data = data.T.unstack().T
data.index.names = ['Date', 'Ticker']
结果的
head()
为: Close High Low Open Volume
Date Ticker
2017-01-03 DXCM 58.250000 59.650002 57.759998 59.500000 1429300.0
SPY 212.796539 213.353941 211.511672 212.607577 91366500.0
2017-01-04 DXCM 60.720001 61.150002 57.680000 58.299999 1811600.0
SPY 214.062500 214.223107 213.146087 213.155529 78744400.0
2017-01-05 DXCM 62.660000 64.550003 60.529999 60.889999 1641400.0
关于python - Pandas :在每行中设置group_by值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60365104/