Peadar08
Peadar08

Reputation: 57

Plotting 2 variables from one column of a dataframe

I have a dataframe with several columns, on of which is gender, (male, female). I'd like to get a line plot of both variable against a third, in this case income, I have the following code:

ax = plt.gca()

df.plot(kind='line', x='income', y=[df.gender=='male'], ax=ax)
df.plot(kind='line', x='income', y=[df.gender =='female'], color='red', ax=ax)

plt.show()

And I keep getting a Value error. I think I should be using a bar chart?? Any thoughts?

Upvotes: 1

Views: 551

Answers (1)

Brandon
Brandon

Reputation: 1018

I would use a groupby+unstack. Assuming you have a male and female observation for each income value, the following code should get you what you want:

df = pd.DataFrame([['male',2,3],
                  ['female',2,8],
                  ['male',5,9],
                  ['female',5,8],
                  ['male',7,4],
                  ['female',7,3]], columns = ['gender', 'income', 'debt'])
df.groupby(['gender', 'income'])['debt'].mean().unstack(level=0).plot(kind='bar')

enter image description here

Upvotes: 1

Related Questions