Python 从数据帧的一列打印2个变量

Python 从数据帧的一列打印2个变量,python,pandas,plot,Python,Pandas,Plot,我有一个数据框,有几列,其中一列是性别(男,女)。我想得到两个变量与第三个变量的折线图,在本例中,我有以下代码: 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() 我不断得到一个值错误。 我想我应该用柱状图?

我有一个数据框,有几列,其中一列是性别(男,女)。我想得到两个变量与第三个变量的折线图,在本例中,我有以下代码:

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()
我不断得到一个值错误。 我想我应该用柱状图??
有什么想法吗?

我会使用groupby+unstack。假设您对每个收入值都有一个男性和女性观察值,下面的代码应该可以满足您的需求:

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')

试着把它改成
df[df.gender='male'].plot(x='income',y='your_field
)`这正是我需要的。谢谢,没问题。如果你的问题解决了,请投票/接受这个答案。