YouTube Data API Tutorial with Python - Analyze the Data - Part 4

Patrick Loeber · Beginner ·🛠️ AI Tools & Apps ·6y ago

Key Takeaways

This video tutorial series demonstrates how to use the YouTube Data API with Python to analyze channel statistics and video data, utilizing tools such as Jupyter notebook, pandas, and matplotlib for data analysis and visualization.

Full Transcript

hey guys welcome to the final part of the YouTube data API tutorial in this part we are going to create a 2x2 notebook and analyze the channel a little bit so last time we learned how we could use the API and download all channel statistics in this file here so then it dumped all the data to this file and here we can see we have the channel statistics like the view count and the subscriber count and how many videos the channel has and then it has the video data part where we have statistics for each of the videos in this channel so here we can see the title the date the description even the thumbnail links then we also have some statistics for example we have the view count and the like count so yeah so let's analyze this JSON file a little bit and for this we are creating a cheap item notebook so let's say to point a notebook and this will open up a notebook if it is installed and if not I would recommend using anaconda to install jupyter and now we can see that we are in this current folder and now let's let's first of all let's make this a little bit larger for you and now let's create a new notebook and I select my virtual environment here so you can see that I have a couple and this one is the PI egg and environment and yeah so what we need here is we want to import Chasen and we also import pandas because we are going to work with pandas here so import pandas SPD and again if you have not installed this then you can do this with either pip install pandas or condom install pandas so I have a whole tutorial about how we can use anaconda and install packages and manage virtual environments so I recommend that you check that out so yeah and now we want to load the file so we say file equals and I will hard-code this year so this file for my channel is called Python engineered of Jason and then let's open this so let's say data equals none and then with open the file and we want to open it in read mode only and let's open this as F and then let's load good chastened data into the data so data equals Jason dot load and here we say simply the file and we can do this because the content in this file here this is well adjacent data so yeah so we can do this and now let's let's have a look at the channel ID and the channel stats so the very first item in this chase an object is the channel ID and then as the very first value in this item we have to channel statistics and the video data so let's extract this so let's say channel ID and step statistics let's say stats equals theta dot pop item so this will return a tuple and as first item we have the key so this is the channel ID and then as value we have the stats and from this that's object we can further extract the two parts so here as a first key we have the channel statistics and as a second part we have the video data so let's extract this so let's let's print the channel ID to see if this is working print channel ID and then I can say cell run cells and yeah it worked so here it printed the channel ID and now let's extract the statistics and the video so let's say channel steps equals stats and then the key is called channel statistics so channel statistics and the video stats part is called video stats equals stats and then as a key we need video data so now we have the channel statistics and the video data so let's run this and create another cell below so we have a key error so channel statistics so yeah if a typo years or run this again and now it's working and now here let's print some information about the channel so here we want to analyze the channel statistics so let's print the views and here the views are inside the channel statistics so channel stats and then as a key we have the view count so again let's have a look here so here let's print the view counts the subscriber count and the video count so let's print the views and let me copy and paste this two times so let's also print the subscriber subscriber and here we need to use the key subscriber count subscribe our count and then let's print the videos so videos or the number of videos in here we have to use video count so let's run this again and insert another cell so yeah so here we can see the views the subscribers and the videos and now let's analyze the video statistics so video statistics and here what I want to do is I want to sort the video data are according to the view count so the tutorials or the videos with the most views should be on top so let's sort this object according to the view count so we can say sorted bits equals and then we use the built in sorted method so this will sort and what should its source so it should sort the video stats object so video underscore stats and then dot item so this will return the dictionary as a key value pair so this will return a tuple where the first value is the key and the second value is the item so the first part is always the video ID and the second part is this dictionary inside here so this is another dictionary and here we have the key view count and we want to sort it according to this so we can do this with a by providing a key so we can say key equals and then this can be a function and for example we can use a lambda here so a lambda is practically a function in one line so lambda item and then what we do here and what we want to do is we want to sort according to this item so we say int so we will convert the view count to a integer and we get the view count by saying item 1 because again this is a key value pair so this is a tuple so we want to have the second part of this tuple which is the actual value in this dictionary and then we can access the key view count so this is how we want to sort it and I also want to say reverse equals true because I want to sort it with the most views in the beginning and by default it's the other way around so it will put the smallest item in the beginning so let's run this and see if this is working here no that's what I expected so here I probably have one parenthesis wrong so this is closing this so I think this was one parenthesis too many so again let's run this and yeah so now it's working so now we have a reversed array so the sort it's pairing year so let's iterate over this and extract only the statistics that we want for now so let's create an empty list and then let's say four bits in sorted bits and then let's extract the video ID so video video ID equals vit zero so again this is the tuple where the first part is the key and the key is always the video ID and then let's get the title so title equals bit and here we have to use the second item in this tuple and then we can extract the key title so again now here we are inside this object so now we can access all the items that we want or all the keys that we want so we can now let's extract the title and then let's extract the statistics here so let's extract the view count and the likes and dislikes and so yeah so let's do this so the title equals video 1 and then as key the title and now let me copy and paste this a couple of times so we also want to have the likes likes equals and here we have to say like count and then let's get the dislike so this likes and then let's get let's use the key this like count and let's also use the comments so comments equals this and then as a key we need to use comment count and let's append all this to our statistics list so let's say stats dot append and now as a list we want to put this in a list where we put the title first then let's put in the view count oh I forgot to use this years old let's do this the same way you like that like counts so let's say copy and paste and then let's say use equals this and s key the view count so this is the key that we used for sorting so let's put this as the second item and then let's put in the like count and then that this or I'm called it likes and views so sorry so views and likes and dislikes and comments so now we have our statistics so now let's run this and create another cell and see if this is working so here yeah we see that kind of a typo again and let's do this one more times around the cell and now it's working no era and now let's put this into a panda's data frame and we can do this by saying data frame equals pandas PD dot data frame and now we want to put in our statistics object and we can specify the column so the columns equals and here again we use the keys so we say we use a list here and the list must match the number of items in our statistics object so here we can say title and then the views and then the likes and then that this likes and last we have the comments and now we have this and now let's simply have a look at the first ten items in this data frame by saying by calling data frame dot hats ten and let's run this and yeah so here it worked and we see that it's sorted all the videos according to the view so the tutorial about cane years neighbors currently the most viewed video in my channel and then we also see the likes and dislikes and the comments so yeah that's pretty nice so yeah you can see the most popular videos and let's create another cell and let's plot this so let's say Oran cell and insert below and then here we say top 10 equals data frame dot hat 10 and then we can plot this with panda so ax equals top 10 and then we can plot this we can call plot and we want a bar plot and si on the x-axis we want to have the title subtitle and on the y-axis we want to have the views and this must match the columns that we specified here so title and views and let's also give this a size so big size equals 12 by 8 and font size to make this a little bit larger for you equals 14 so now let's run this type arrow or we have an arrow here because we have a type error and this is because we did not convert this to a integer here so we have to so this is currently we have the views and the likes and the dislikes and the comments but this is a string right now so we have to convert this to a integer the same way as we did up here so this should be wrapped in this integer and then it should work so int and again the comments should be an int so now this is working so now let's run all and then down here yeah it should create the plot so here we plotted the 10 most viewed videos and the titles so it's the first video we see that we have the K nearest neighbor and then the naive by yes and yeah so this gives a pretty good impression about the view count of the top 10 videos and now let's do the same thing with the least viewed video so let me grab this cell here and simply copy and paste this and but now we don't want the top 10 but we want to have the bottom 10 and we can get the spice data frame dot tail and then we say bottom turn plot so bottom 10 so now let's run this and it's working again and then yeah we see that the list comprehension tutorial currently has the least viewed videos and the YouTube data API tutorial part 1 also is not so much views but this is because it's one of the newest tutorials in my channel so yeah it's only been out for like a week so yeah so now we plotted the top 10 and the bottom 10 videos and let's further analyze some statistics for example we can analyze the like and dislike ratio so let's say likes vs. dislikes and so let's get the like so can let's get the average likes likes average equals off the top 10 so top 10 and we can access this with the key like so key likes and then calculate the mean so we can automatically do this with pandas and then let's do the same thing with the dislikes and also with the comments so let's say this likes and it's a key we want to use dislikes and then also the comments average so comments average equals this and here comments and then let's copy and paste this whole thing or first of all let's print this so let's print let's say top 10 and then let's print the mmm let's print the average like so average likes and then we want to use the likes average and the same for all the others so again copy and paste this two times so average likes average dislikes so this likes average and average comments and here comments and let's run this and we see it's working so now the average likes are 46 for the top ten and the average dislike only 1.4 and about twelve point four comments for each of the top ten videos so now let's do the same with the bottom ten videos so copy and paste and here we use bottom ten bottom ten and bottom ten and then we print the bottom ten statistics so now let's run this again and here we see that we only have seven likes on average for each of the videos but also not so much dislikes but also not so much comments so yeah so this is one thing that we can analyze and of course you can dig even deeper into the statistics for example you could analyze the titles and compare them so which titles are good or for example you can also analyze the description so then you can find out what the YouTube algorithm needs to the chest for others or you can analyze this thumbnails and find out which thumbnails are good for the YouTube algorithm so that it can suggest it to others and also for the click rate so yeah so there's so much that we can do with this chastened data and now you get an impression how you can work with two pi the notebook and analyze this and yeah I hope you enjoyed this and if you like the serious then please subscribe to the channel and see you next time bye

Original Description

In this Python Tutorial we will be learning how to work with the YouTube Data API and analyze channel statistics. This is going to be a 4 part series: - Part 1: Setup your app and get API Key, and extract basic channel statistics - Part 2: Get a list of all the videos of a channel - Part 3: Get in depth statistics for each video - Part 4: Analyze the data in a jupyter notebook ~~~~~~~~~~~~~~ GREAT PLUGINS FOR YOUR CODE EDITOR ~~~~~~~~~~~~~~ ✅ Write cleaner code with Sourcery: https://sourcery.ai/?utm_source=youtube&utm_campaign=pythonengineer * 📚 Get my FREE NumPy Handbook: https://www.python-engineer.com/numpybook 📓 Notebooks available on Patreon: https://www.patreon.com/patrickloeber ⭐ Join Our Discord : https://discord.gg/FHMg9tKFSN If you enjoyed this video, please subscribe to the channel! The code can be found here: https://github.com/patloeber/youtube-analyzer More: - Official guides and docs: https://developers.google.com/youtube/v3 - Register app: https://console.developers.google.com/ You can find me here: Website: https://www.python-engineer.com Twitter: https://twitter.com/patrickloeber GitHub: https://github.com/patrickloeber #Python #Coding #Tutorial ---------------------------------------------------------------------------------------------------------- * This is a sponsored link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Patrick Loeber · Patrick Loeber · 58 of 60

1 Lists in Python - Advanced Python 01 - Programming Tutorial
Lists in Python - Advanced Python 01 - Programming Tutorial
Patrick Loeber
2 Tuples in Python - Advanced Python 02 - Programming Tutorial
Tuples in Python - Advanced Python 02 - Programming Tutorial
Patrick Loeber
3 Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Patrick Loeber
4 Sets in Python - Advanced Python 04 - Programming Tutorial
Sets in Python - Advanced Python 04 - Programming Tutorial
Patrick Loeber
5 Strings in Python - Advanced Python 05 - Programming Tutorial
Strings in Python - Advanced Python 05 - Programming Tutorial
Patrick Loeber
6 Collections in Python - Advanced Python 06 - Programming Tutorial
Collections in Python - Advanced Python 06 - Programming Tutorial
Patrick Loeber
7 Itertools in Python - Advanced Python 07 - Programming Tutorial
Itertools in Python - Advanced Python 07 - Programming Tutorial
Patrick Loeber
8 Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Patrick Loeber
9 Exceptions in Python - Advanced Python 09 - Programming Tutorial
Exceptions in Python - Advanced Python 09 - Programming Tutorial
Patrick Loeber
10 Logging in Python - Advanced Python 10 - Programming Tutorial
Logging in Python - Advanced Python 10 - Programming Tutorial
Patrick Loeber
11 JSON in Python - Advanced Python 11 - Programming Tutorial
JSON in Python - Advanced Python 11 - Programming Tutorial
Patrick Loeber
12 Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Patrick Loeber
13 Decorators in Python - Advanced Python 13 - Programming Tutorial
Decorators in Python - Advanced Python 13 - Programming Tutorial
Patrick Loeber
14 Generators in Python - Advanced Python 14 - Programming Tutorial
Generators in Python - Advanced Python 14 - Programming Tutorial
Patrick Loeber
15 Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Patrick Loeber
16 Threading in Python - Advanced Python 16 - Programming Tutorial
Threading in Python - Advanced Python 16 - Programming Tutorial
Patrick Loeber
17 Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Patrick Loeber
18 Function arguments in detail - Advanced Python 18 - Programming Tutorial
Function arguments in detail - Advanced Python 18 - Programming Tutorial
Patrick Loeber
19 The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
Patrick Loeber
20 Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Patrick Loeber
21 Context Managers in Python - Advanced Python 21 - Programming Tutorial
Context Managers in Python - Advanced Python 21 - Programming Tutorial
Patrick Loeber
22 KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
Patrick Loeber
23 Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Patrick Loeber
24 Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Patrick Loeber
25 Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Patrick Loeber
26 Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Patrick Loeber
27 Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Patrick Loeber
28 SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
Patrick Loeber
29 Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Patrick Loeber
30 Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Patrick Loeber
31 Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Patrick Loeber
32 PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
Patrick Loeber
33 K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
Patrick Loeber
34 Anaconda Tutorial - Installation and Basic Commands
Anaconda Tutorial - Installation and Basic Commands
Patrick Loeber
35 PyTorch Tutorial 01 - Installation
PyTorch Tutorial 01 - Installation
Patrick Loeber
36 PyTorch Tutorial 02 - Tensor Basics
PyTorch Tutorial 02 - Tensor Basics
Patrick Loeber
37 PyTorch Tutorial 03 - Gradient Calculation With Autograd
PyTorch Tutorial 03 - Gradient Calculation With Autograd
Patrick Loeber
38 PyTorch Tutorial 04 - Backpropagation - Theory With Example
PyTorch Tutorial 04 - Backpropagation - Theory With Example
Patrick Loeber
39 PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
Patrick Loeber
40 PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
Patrick Loeber
41 PyTorch Tutorial 07 - Linear Regression
PyTorch Tutorial 07 - Linear Regression
Patrick Loeber
42 PyTorch Tutorial 08 - Logistic Regression
PyTorch Tutorial 08 - Logistic Regression
Patrick Loeber
43 PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
Patrick Loeber
44 PyTorch Tutorial 10 - Dataset Transforms
PyTorch Tutorial 10 - Dataset Transforms
Patrick Loeber
45 Download Images With Python Automatically - Python Web Scraping Tutorial
Download Images With Python Automatically - Python Web Scraping Tutorial
Patrick Loeber
46 PyTorch Tutorial 11 - Softmax and Cross Entropy
PyTorch Tutorial 11 - Softmax and Cross Entropy
Patrick Loeber
47 Select Movies with Python - Web Scraping Tutorial
Select Movies with Python - Web Scraping Tutorial
Patrick Loeber
48 PyTorch Tutorial 12 - Activation Functions
PyTorch Tutorial 12 - Activation Functions
Patrick Loeber
49 List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
Patrick Loeber
50 PyTorch Tutorial 13 - Feed-Forward Neural Network
PyTorch Tutorial 13 - Feed-Forward Neural Network
Patrick Loeber
51 How To Add A Progress Bar In Python With Just One Line - Python Tutorial
How To Add A Progress Bar In Python With Just One Line - Python Tutorial
Patrick Loeber
52 PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
Patrick Loeber
53 The Walrus Operator - New in Python 3.8 - Python Tutorial
The Walrus Operator - New in Python 3.8 - Python Tutorial
Patrick Loeber
54 PyTorch Tutorial 15 - Transfer Learning
PyTorch Tutorial 15 - Transfer Learning
Patrick Loeber
55 YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
Patrick Loeber
56 YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
Patrick Loeber
57 YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
Patrick Loeber
YouTube Data API Tutorial with Python - Analyze the Data - Part 4
YouTube Data API Tutorial with Python - Analyze the Data - Part 4
Patrick Loeber
59 AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
Patrick Loeber
60 Ultimate FREE Study Guide for Machine Learning and Deep Learning
Ultimate FREE Study Guide for Machine Learning and Deep Learning
Patrick Loeber

This tutorial series teaches how to use the YouTube Data API with Python to analyze channel statistics and video data, covering topics such as data analysis, visualization, and machine learning. By following this tutorial, viewers can learn how to extract and visualize YouTube data, calculate statistics, and gain insights into video performance.

Key Takeaways
  1. Import necessary libraries such as pandas and Chasen
  2. Load JSON file using Chasen
  3. Extract channel ID and statistics from JSON data
  4. Sort video data according to view count
  5. Create a pandas data frame to store video statistics
  6. Plot the top 10 and bottom 10 videos by view count
  7. Calculate average likes, dislikes, and comments for top 10 and bottom 10 videos
💡 The YouTube Data API can be used to extract valuable insights into YouTube channel and video performance, and by leveraging Python libraries such as pandas and matplotlib, users can analyze and visualize this data to inform their marketing and content strategies.

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →