TourEiffel
TourEiffel

Reputation: 4444

How to plot a multi-indexed dataframe

I have a dataframe, DF:

          Data1  Data2
2022/7/8  3      3
2022/7/7  4      2
2022/7/6  5      1
2022/7/5  6      3
2022/7/4  7      2

Doing the following,

sns.lineplot(x=DF.index, y=DF["Data1"], ax=Myax)

throws the error:

ValueError: Data must be 1-dimensional

What am I doing wrong there?

Below is the requested output:

{('USGG10YR Index', 'PX_LAST'): {datetime.date(2018, 12, 31): 2.6842, datetime.date(2019, 1, 1): 2.6842, datetime.date(2019, 1, 2): 2.6204, datetime.date(2019, 1, 3): 2.5535, datetime.date(2019, 1, 4): 2.6677}, ('GTFRF10YR Corp', 'PX_LAST'): {datetime.date(2018, 12, 31): 0.705, datetime.date(2019, 1, 1): 0.705, datetime.date(2019, 1, 2): 0.648, datetime.date(2019, 1, 3): 0.651, datetime.date(2019, 1, 4): 0.697}, ('GTDEM10YR Corp', 'PX_LAST'): {datetime.date(2018, 12, 31): 0.239, datetime.date(2019, 1, 1): 0.239, datetime.date(2019, 1, 2): 0.164, datetime.date(2019, 1, 3): 0.151, datetime.date(2019, 1, 4): 0.206}, ('GTITLII10Y Govt', 'PX_LAST'): {datetime.date(2018, 12, 31): 1.811, datetime.date(2019, 1, 1): 1.811, datetime.date(2019, 1, 2): 1.782, datetime.date(2019, 1, 3): 1.947, datetime.date(2019, 1, 4): 1.974}}

Upvotes: 1

Views: 91

Answers (1)

BigBen
BigBen

Reputation: 50162

Your columns are a MultiIndex, so reference accordingly:

sns.lineplot(
    x=TenYearGovYieldHist.index, 
    y=TenYearGovYieldHistf[('USGG10YR Index', 'PX_LAST')], 
    ax=Myax
)

Or using droplevel:

sns.lineplot(
    x=TenYearGovYieldHist.index, 
    y=TenYearGovYieldHist.droplevel(1, axis=1)['USGG10YR Index'],
    ax=Myax
)

Upvotes: 3

Related Questions