1

I have 2 dataframes with the same column names, index and size. I want to create a scatterplot of one dataframe column vs the other with the same header. When I try the code below, only the sim.columns are looping, while the obs.columns plots only the first column and don't loop. So what I get are scatter plots of each sim.columns against the first obs.column only. I'm not sure what is messed up with this loop. Thanks for your help!

    obs= pd.read_csv(obsFile)
    obs.rename(columns={obs.columns[0]: "SP" }, inplace = True)
    sim= pd.read_csv(simFile)
    sim.rename(columns={sim.columns[0]: "SP" }, inplace = True)
    
sim = sim.set_index("SP")
obs = obs.set_index("SP")

for colsim in sim.columns:
    for colobs in obs.columns:
        axes = plt.gca()
        axes.set_xlim([1,630])
        plt.scatter(sim.index, sim[colsim])
        plt.scatter(obs.index, obs[colobs])

        plt.xlabel('Stress Period')
        plt.ylabel('groundwater elevation(m)')
        plt.title(str(colsim))
        plt.savefig(os.path.join(outFold, str(colsim)+'.pdf')) 
        plt.close()
        break

2 Answers 2

1

Instead of a double for loop:

for colsim in sim.columns:
    for colobs in obs.columns:
        ...

Just use a zip:

for colsim, colobs in zip(sim.columns, obs.columns):
    ...

Not sure what you mean with what you wanna do, but if this code doesn't work, try removing the break then it should work, but if it works without removing the break just keep it.

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

1 Comment

zip is different from double loop ~
0

You shouldn't use a nested loop here, it does not what you want to achieve. Instead you want to loop through both dataframes at the same time in one loop. i can think of 2 ways how to achieve this:

  1. merge the dataframes and loop through the merged dataframe in one loop
  2. introduce a counter variable which can be used as an index for looping through both dataframes parallel

2 Comments

Just using zip is enough... Like in my answer
you are right, i used the wrong term. by merging i meant zipping.

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.