2

I have double index in Panda's dataframe like the example below.

           c         d
a  b         
1  3   1.519970 -0.493662
2  4   0.600178  0.274230
3  5   0.132885 -0.023688
4  6   2.410179  1.450520

How do I plot column 'c' as y-axis and index 'b' as x-axis. With one index, it is easy to plot. But I have trouble with multi index plotting. Thank you for any help!!.

6
  • Did you try df.reset_index().plot(x="b",y="c")? Commented Mar 14, 2018 at 23:32
  • Or, df.reset_index(level=0, drop=True).c.plot() Commented Mar 14, 2018 at 23:37
  • Yes. I tried it. I have a very big data set. I was wondering if there is any other way without using reset_index() everytime? Thank you .. Commented Mar 14, 2018 at 23:43
  • If your dataset is so big that calling reset_index is an issue, you will have much larger issues when actually plotting it; or in other words, do not plot any data that you cannot affort to reshape. Commented Mar 14, 2018 at 23:46
  • Okay. Thanks for the suggestion. If there is no other way, I will stick with using reset_index(). Really appreciate the help. Thank you guys. Commented Mar 14, 2018 at 23:47

1 Answer 1

4

Option 1
Two options have been provided (in the comments) involving reset_index. They are

df.reset_index().plot(x="b",y="c")

Or,

df.reset_index(level=0, drop=True).c.plot()

Both of these should work as expected, but will become expensive for large dataframes.


Option 2
If you are worried about memory, here's an option that does not involve resetting the index:

plt.plot(df.index.get_level_values(1), df.c)

reset_index generates copies of the data. This is more efficient, because it doesn't have to.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.