我正在寻找的df示例最终结果如下,调整后的收盘价作为数据:
# GE JPM MSFT PG
#date
#2015-01-02 25.06 62.49 46.760 90.44
#2015-01-05 24.60 60.55 46.325 90.01
#2015-01-06 24.07 58.98 45.650 89.60
#2015-01-07 24.08 59.07 46.230 90.07
#2015-01-08 24.37 60.39 47.590 91.10
<小时 />import yfinance as yf
import datetime
import pandas as pd
import pandas_datareader as pdr
def get(tickers, startdate, enddate):
def data(ticker):
return (pdr.get_data_yahoo(ticker, start=startdate, end=enddate))
datas = map (data, tickers)
return(pd.concat(datas, keys=tickers, names=['Ticker', 'Date']))
ticker = ['GE' , 'JPM' , 'MSFT' , 'PG']
all_data = get(ticker, datetime.datetime(2018, 10, 1), datetime.datetime(2020, 1, 1))
print (all_data)
High Low ... Volume Adj Close
Ticker Date ...
GE 2018-10-01 229.419998 226.350006 ... 23600800.0 221.988266
2018-10-02 230.000000 226.630005 ... 24788200.0 223.961426
2018-10-03 233.470001 229.779999 ... 28654800.0 226.686707
2018-10-04 232.350006 226.729996 ... 32042000.0 222.701340
2018-10-05 228.410004 220.580002 ... 33580500.0 219.087158
... ... ... ... ... ...
JPM 2019-12-24 1350.260010 1342.780029 ... 347500.0 1343.560059
2019-12-26 1361.327026 1344.469971 ... 667500.0 1360.400024
2019-12-27 1364.530029 1349.310059 ... 1038400.0 1351.890015
2019-12-30 1353.000000 1334.020020 ... 1050900.0 1336.140015
2019-12-31 1338.000000 1329.084961 ... 961800.0 1337.020020
上面的代码将股票代码存储在行而不是列中。
我只需要每个股票代码的调整后的收盘价列。我知道如何将"调整后的收盘价"列提取到另一个熊猫系列中,但我陷入了如何进一步进行的问题。
你可以在这里使用pivot
:
def get(tickers, startdate, enddate):
def data(ticker):
return (pdr.get_data_yahoo(ticker, start=startdate, end=enddate))
datas = map (data, tickers)
return(pd.concat(datas, keys=tickers, names=['Ticker', 'Date']))
ticker = ['GE' , 'JPM' , 'MSFT' , 'PG']
all_data = get(ticker, datetime.datetime(2018, 10, 1), datetime.datetime(2020, 1, 1))
df = all_data['Adj Close'].to_frame().reset_index()
print(df.pivot(index='Date', columns='Ticker'))
Adj Close
Ticker GE JPM MSFT PG
Date
2018-10-01 11.550495 107.484100 112.812271 79.660583
2018-10-02 11.770231 107.929184 112.363396 80.317520
2018-10-03 11.923093 108.942482 112.382904 79.051262
2018-10-04 12.095059 109.924706 110.060501 77.994453
2018-10-05 12.591855 109.304848 109.416473 78.213432
... ... ... ... ...
2019-12-24 11.178899 135.296478 156.515396 123.655876
2019-12-26 11.218859 136.732239 157.798309 123.655876
2019-12-27 11.168909 136.830582 158.086731 124.515007
2019-12-30 11.069008 136.329041 156.724243 122.915245
2019-12-31 11.148929 137.086258 156.833633 123.339874
您正在寻找未分组数据pivot_table()
。
东风
Ticker Date High Low Volume Adj Close
0 GE 10/1/2018 229.419998 226.350006 23600800 221.988266
1 GE 10/2/2018 230.000000 226.630005 24788200 223.961426
2 GE 10/3/2018 233.470001 229.779999 28654800 226.686707
3 GE 10/4/2018 232.350006 226.729996 32042000 222.701340
4 GE 10/5/2018 228.410004 220.580002 33580500 219.087158
5 JPM 12/24/2019 1350.260010 1342.780029 347500 1343.560059
6 JPM 12/26/2019 1361.327026 1344.469971 667500 1360.400024
7 JPM 12/27/2019 1364.530029 1349.310059 1038400 1351.890015
8 JPM 12/30/2019 1353.000000 1334.020020 1050900 1336.140015
9 JPM 12/31/2019 1338.000000 1329.084961 961800 1337.020020
df.pivot_table(index='Date', columns='Ticker', values='Close')
输出
Ticker GE JPM
Date
10/1/2018 221.988266 NaN
10/2/2018 223.961426 NaN
10/3/2018 226.686707 NaN
10/4/2018 222.701340 NaN
10/5/2018 219.087158 NaN
12/24/2019 NaN 1343.560059
12/26/2019 NaN 1360.400024
12/27/2019 NaN 1351.890015
12/30/2019 NaN 1336.140015
12/31/2019 NaN 1337.020020