【问题标题】:plot the data based the total count of two columns [duplicate]根据两列的总数绘制数据[重复]
【发布时间】:2021-01-31 18:26:33
【问题描述】:

我有一个这样的数据集:

df = pd.DataFrame({'name':["a"," b", "c","d", "e"],
               'gender': ["m", "f", "f", "f", "m"],
              'year':[ 2018, 2019, 2020, 2017, 2014],
              'count':[100, 30, 10, 90,34]})

我想将男性和女性的总数绘制为一年的函数。 输出必须如下所示:

【问题讨论】:

  • 你尝试了什么,为什么失败了?另外:你的df中的名字是什么?不相关的列?

标签: python pandas plot data-analysis


【解决方案1】:

您可以创建数据透视表并直接绘图。下面是一个以 bar 作为结果的示例。行在这里不是个好主意,因为您只有几年时间:

import matplotlib.pyplot as plt
pd.pivot_table(df, index='year', columns=['gender'], values='count', aggfunc='sum').plot.bar()
plt.show()

输出:

【讨论】:

    猜你喜欢
    • 2018-07-22
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多