如何熔化多个索引
How to melt multiple index
我有数据框,有多个索引,但我想融化保留它的索引。
这是我的:
-汇总 DF:
Attributes Adj Close|Close
Symbols AALR3.SA | ABCB4.SA
Date
2019-12-09| 17.862261|17.999647
2019-12-10| 17.852343|18.240801
2019-12-11| 18.001112|18.298676
-图片:
-我为获得此 df 所做的代码:
Brazilian_stock_list1 = ['AALR3.SA','ABCB4.SA']
brazilian_stocks1 = pdr.get_data_yahoo(Brazilian_stock_list1, start=start_date, end=end_date)
razilian_stocks1.head()
-我试过了,但它删除了索引“日期”:
pd.melt(brazilian_stocks1, value_vars=teste.columns.tolist())
-最后,这就是我需要的:
date Attributes Symbols value
2019-12-09 Adj Close AALR3.SA 17.862.261
2019-12-10 Adj Close AALR3.SA 17.852.343
2019-12-11 Adj Close AALR3.SA 18.001.112
2019-12-12 Adj Close AALR3.SA 18.229.223
在此先感谢您为我提供的帮助。
让我们试试 stack
然后 reset_index
:
df.stack(level=(0,1)).reset_index(name='value')
输出:
Date Attribute Symbols value
0 2019-12-09 Adj Close AALR3.SA 17.862261
1 2019-12-09 Close ABCB4.SA 17.999647
2 2019-12-10 Adj Close AALR3.SA 17.852343
3 2019-12-10 Close ABCB4.SA 18.240801
4 2019-12-11 Adj Close AALR3.SA 18.001112
5 2019-12-11 Close ABCB4.SA 18.298676
我有数据框,有多个索引,但我想融化保留它的索引。
这是我的:
-汇总 DF:
Attributes Adj Close|Close
Symbols AALR3.SA | ABCB4.SA
Date
2019-12-09| 17.862261|17.999647
2019-12-10| 17.852343|18.240801
2019-12-11| 18.001112|18.298676
-图片:
-我为获得此 df 所做的代码:
Brazilian_stock_list1 = ['AALR3.SA','ABCB4.SA']
brazilian_stocks1 = pdr.get_data_yahoo(Brazilian_stock_list1, start=start_date, end=end_date)
razilian_stocks1.head()
-我试过了,但它删除了索引“日期”:
pd.melt(brazilian_stocks1, value_vars=teste.columns.tolist())
-最后,这就是我需要的:
date Attributes Symbols value
2019-12-09 Adj Close AALR3.SA 17.862.261
2019-12-10 Adj Close AALR3.SA 17.852.343
2019-12-11 Adj Close AALR3.SA 18.001.112
2019-12-12 Adj Close AALR3.SA 18.229.223
在此先感谢您为我提供的帮助。
让我们试试 stack
然后 reset_index
:
df.stack(level=(0,1)).reset_index(name='value')
输出:
Date Attribute Symbols value
0 2019-12-09 Adj Close AALR3.SA 17.862261
1 2019-12-09 Close ABCB4.SA 17.999647
2 2019-12-10 Adj Close AALR3.SA 17.852343
3 2019-12-10 Close ABCB4.SA 18.240801
4 2019-12-11 Adj Close AALR3.SA 18.001112
5 2019-12-11 Close ABCB4.SA 18.298676