熊猫扁平多列索引

时间:2019-01-15 02:53:30

标签: python pandas seaborn yahoo-finance

所以我一直在使用pandas.datareader和yahoo Finance,想知道是否有一种快速简便的解决方案来重新格式化从yahoo finance返回的多索引数据框,以反映随提供的fmri样本数据的格式海运。到目前为止,我已经能够通过Yahoo Finance在数据框上调用reset_index()进行折线图绘制,以便将日期作为seaborn y轴的列。

from pandas_datareader import data as pdr
import pandas as pd
import fix_yahoo_finance as yf
import seaborn as sns
import matplotlib.pyplot as plt
yf.pdr_override() # <== that's all it takes :-)
tags = [ "AMZN","GOOG"]
dateIndexedData = pdr.get_data_yahoo(tags, start="2018-11-01", end="2018-11-30")
d = dateIndexedData.reset_index()
for k in tags:
    ax = sns.lineplot(x="Date", y=("Close",k), data=d)
plt.show()

Amazon and Google stocks from November 2018

但是,我想有一列来指示与每一行相对应的库存。我知道要在Seaborn的绘图函数中使用hue参数,我需要一个高表而不是一个宽表,在该表中,我从熊猫数据读取器获得的数据的第二级多索引将显示为一列。就像seaborn referenced here提供的FMRI数据一样。

enter image description here

0 个答案:

没有答案