-2

I have a dataset like this:

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]})

I want to Plot the total number of males and females as a function of the year. the output must look like this: enter image description here

Mr. T
  • 11,960
  • 10
  • 32
  • 54
nemo92world
  • 101
  • 8
  • 2
    What have you tried, why did it fail? Also: What is name in your df? An irrelevant column? – Mr. T Jan 31 '21 at 18:30

1 Answers1

1

You can create a pivot table and plot directly. Below is an example with bar as result. Line is not good idea here as you have few years only:

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

Output:

enter image description here

IoaTzimas
  • 10,538
  • 2
  • 13
  • 30