绘制Pandas Dataframe列之间的关系

时间:2017-01-11 00:14:49

标签: python pandas dataframe

我的Pandas Dataframe有多列。目前我有两列我感兴趣,它们看起来像这样:

DataFrame data

我想绘制一个带有黄色实例行数的条形图,按每个年龄细分:

Bar Graph - Yellow Color count per Age

3岁时黄色3个实例,15岁时黄色实例1个。

我能从熊猫那里得到我想要的东西,如果是这样的话怎么样?我对Pandas和这个知识领域都很陌生,所以任何指针都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

考虑数据框df

y, r, b, o = 'Yellow', 'Red', 'Blue', 'Orange'
df = pd.DataFrame(dict(
    Color=[y, r, b, y, o, r, y, y],
    Age=[10, 15, 20, 10, 20, 15, 15, 10]
))

df.groupby(['Color', 'Age']).size().loc[y].plot.bar()

enter image description here

有很多方法可以获得相同的数据 另一个例子

df.groupby('Color').Age.value_counts().loc['Yellow'].plot(kind='bar')