我正在尝试
数据如下:
Attributes Adj Close Close High Low Open Volume
Symbols AAPL ACN AAPL ACN AAPL ACN AAPL ACN AAPL ACN AAPL ACN
Date
2002-01-02 1.444668 19.703959 1.664286 26.209999 1.664286 26.910000 1.568571 25.950001 1.575000 26.900000 132374200.0 900500.0
2002-01-03 1.462029 19.087507 1.684286 25.389999 1.696429 26.270000 1.626429 25.299999 1.642857 26.230000 153001800.0 698200.0
2002-01-04 1.468849 20.824106 1.692143 27.700001 1.710714 28.200001 1.642143 25.240000 1.667143 25.389999 102494000.0 2708500.0
我尝试使用melt,但以某种方式只能使它与一列Adj Close一起使用:
Date Ticker Adj Close
0 2002-01-02 AAPL 1.444668
1 2002-01-03 AAPL 1.462029
2 2002-01-04 AAPL 1.468849
3 2002-01-07 AAPL 1.419867
4 2002-01-08 AAPL 1.401886
代码是:
df = pd.melt(df_market_data['Adj Close'].reset_index(), id_vars='Date', value_name='Adj Close', var_name='minor').rename(columns={'minor': 'Ticker'})
尝试与所有人一起使用时
Adj Close Close High Low Open Volume
它不起作用:
df = pd.melt(df_market_data.reset_index(), id_vars='Date', value_name=['Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume'], var_name='minor').rename(columns={'minor': 'Ticker'})
代码如下:
import pandas as pd
import pandas_datareader.data as pdr
import datetime as dt
ticker_list = ['AAPL','ACN']
start_date = dt.datetime(2002,1,1).date()
end_date = dt.datetime.now().date() - dt.timedelta(days=1)
df_market_data = pdr.DataReader(ticker_list, 'yahoo', start_date, end_date)
df = pd.melt(df_market_data['Adj Close'].reset_index(), id_vars='Date', value_name='Adj Close', var_name='minor').rename(columns={'minor': 'Ticker'})
答案 0 :(得分:0)
stack()函数用于将指定的级别从列堆叠到索引。在这里,一级索引用于接收适当的输出和专用数据帧输出
df = df_market_data.stack(level = 1).reset_index(level = [0, 1], drop = False).reset_index()