What I want to do is to apply the plot function only to certain selection of records in a dataset. Let's say, I have a column of length (numeric), a column of body mass (numeric) and a column of sex (Male or Female). I want to scatterplot the length vs the mass, but only for Male, so that Female won't appear on the plot. How do I do that?
Asked
Active
Viewed 1,118 times
1 Answers
1
First you need to filter your dataframe to provide information only for the rows for which sex
is Male
. This will be the input for your plot.
df_1 = df.loc[df['sex'].eq('Male')]
Once you have done the filtering, you need to add the plotting syntax which is as @Quang Hoang commented .plot.scatter(x='length',y='mass')
which will be the values for your x
and y
axis. In a single line:
df_1 = df.loc[df['sex'].eq('Male')].plot.scatter(x='length',y='mass')
EDIT: If there were more columns in your dataframe and you wish to only keep those two (length and mass) then your filtering should be done with:
df_1 = df.loc[df['sex'].eq('Male'),['length','mass']]

Celius Stingher
- 17,835
- 6
- 23
- 53
-
thank you for your explanation. what if there is more than two columns like Length, Mass, Height, Weight etc? – rmb Nov 27 '19 at 17:40
-
I will edit my answer in second to example that case. However if there were to be more it shouldn't be any issue with the code provided. Edit done. – Celius Stingher Nov 27 '19 at 17:41
-
maybe i was not clear what i was asking is what if i want to plot more than two columns for male. i am sorry i am just learning this. – rmb Nov 27 '19 at 17:51
-
For multiple columns you should make multiple plots (because of scatterplots being 2D), here is a question that adresses your question: https://stackoverflow.com/questions/43061768/plotting-multiple-scatter-plots-pandas . – Celius Stingher Nov 27 '19 at 17:51