1

I'm a beginner with pandas and python in general. I would like to make a scatter plot of the column "vol" in a way that the values in 'x' axis are the ones that correspond to '1' in the column "reg", and 'y' axis are values of "vol" that correspond to '0' in "reg"

I'd appreciate your help.

vol dx reg
4324.208797 CN 1
3805.078032 CN 1
3820.867115 CN 1
3657.034962 CN 1
3967.540763 CN 1
202822.164817 MCI 0
240965.499488 MCI 0
258301.119915 MCI 0
220183.190232 MCI 0
212202.300552 MCI 0

2 Answers 2

1

Try the following:

import matplotlib.pyplot as plt

plt.scatter(df[df.reg==1]['vol'], df[df.reg==0]['vol'])

plt.show()
Sign up to request clarification or add additional context in comments.

Comments

1

An alternative is to use pandas even for plotting as matplotlib requires too much work. But to do that, dataframe needs to be transformed into wide format for pandas plotting to work. Then plotting comes for free:

vol = df.groupby('reg').apply(lambda g: g.reset_index(drop=True)).unstack('reg')['vol']
vol.plot(kind='scatter',x=1,y=0, title='scatter plot')

groupby...reset simply provides a common index across reg series 0..4. unstack transforms it into columns/wide format that works with pandas. Here is the final dataframe that is ready for pandas plotting.
enter image description here

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.