0

I have some code which plots variables with the same names in 2 different dataframes;

    ax1.plot(df2[varname],'y-o',label='2')
    ax1.plot(df1[varname],'g-o',label='1')

in some cases df2 may not have the variable, in which case I just want to chart the variable in df1, so just one line, and not 2.

This however, produces an error if the variable isn't in both dataframes. Is there an obvious solution I'm missing?

1 Answer 1

1

Just test that varname exists in df2:

if(varname in df2.columns):
    ax1.plot(df2[varname],'y-o',label='2')
ax1.plot(df1[varname],'g-o',label='1')
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.