【问题标题】:Charting gender in IPython notebook using pandas or matplotlib使用 pandas 或 matplotlib 在 IPython 笔记本中绘制性别图表
【发布时间】:2014-10-02 10:38:57
【问题描述】:

我有一个带有 Gender 列的 Pandas 数据框,其中包含值 M 或 F。我想将所有 M 和 F 的计数绘制为带有 2 个条形的条形图或带有 2 个楔形的饼图。

到目前为止,我已经尝试过 hist,但它出错了:

data['GENDER'].hist()
TypeError: 不能连接 'str' 和 'float' 对象

也许我需要进行分组?

【问题讨论】:

  • 数据是什么样的?

标签: python matplotlib charts pandas ipython


【解决方案1】:

您可能想要这个 - groupby/size 来获取每个性别的计数,并使用 .plot.hist (这只对数字数据有意义)。

df.groupby('GENDER').size().plot(kind='bar')

【讨论】:

  • 谢谢!有趣的是,在看到你的答案之前,我正在尝试 df.groupby('GENDER').count().plot()。
猜你喜欢
  • 2013-10-24
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 2016-03-18
  • 2016-05-26
  • 2014-07-16
相关资源
最近更新 更多