Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I plot the frequency of an event overtime with pandas?I

I was trying to plot some data from a pandas dataframe.
My table contains 10000ish films and for each of them two info: the year it got published, and a rating from 0 to 3.
I am having a hard time trying to plot a graph with the pandas library that shows the number of films that received a particular rating (3 in my case) every year.

I have tried to use .value_counts(), but it didn’t work as i hoped, since I can’t isolate a single value, maintaining the rating linked to its year.

I really hoped i decently explained my problem, since it is the first time i ask help on stack overflow.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is the code i used to get my dataframe, if it is useful in any way.

import json
import requests
import pandas as pd
import numpy as np

request = requests.get("http://bechdeltest.com/api/v1/getAllMovies").json()
data = pd.DataFrame(request)

P.S. Thank you for the precious help!

>Solution :

You can filter by rating and use Series.value_counts:

s = data.loc[data['rating'].eq(3), 'year'].value_counts()

But there is many years of films:

print (len(s))
108

So for plot I filter only counts greatwer like 30, it is 40 years here:

print (s.gt(30).sum())
40

So filter again and plot:

s[s.gt(30)].plot.bar()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading