!pip install google-api-python-client
Requirement already satisfied: google-api-python-client in c:\users\dondr\anaconda3\lib\site-packages (1.7.11) Requirement already satisfied: six<2dev,>=1.6.1 in c:\users\dondr\appdata\roaming\python\python39\site-packages (from google-api-python-client) (1.16.0) Requirement already satisfied: google-auth>=1.4.1 in c:\users\dondr\anaconda3\lib\site-packages (from google-api-python-client) (2.15.0) Requirement already satisfied: httplib2<1dev,>=0.9.2 in c:\users\dondr\anaconda3\lib\site-packages (from google-api-python-client) (0.21.0) Requirement already satisfied: uritemplate<4dev,>=3.0.0 in c:\users\dondr\anaconda3\lib\site-packages (from google-api-python-client) (3.0.1) Requirement already satisfied: google-auth-httplib2>=0.0.3 in c:\users\dondr\anaconda3\lib\site-packages (from google-api-python-client) (0.0.3) Requirement already satisfied: cachetools<6.0,>=2.0.0 in c:\users\dondr\anaconda3\lib\site-packages (from google-auth>=1.4.1->google-api-python-client) (5.2.0) Requirement already satisfied: pyasn1-modules>=0.2.1 in c:\users\dondr\anaconda3\lib\site-packages (from google-auth>=1.4.1->google-api-python-client) (0.2.8) Requirement already satisfied: rsa<5,>=3.1.4 in c:\users\dondr\anaconda3\lib\site-packages (from google-auth>=1.4.1->google-api-python-client) (4.9) Requirement already satisfied: pyparsing!=3.0.0,!=3.0.1,!=3.0.2,!=3.0.3,<4,>=2.4.2 in c:\users\dondr\appdata\roaming\python\python39\site-packages (from httplib2<1dev,>=0.9.2->google-api-python-client) (3.0.9) Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in c:\users\dondr\anaconda3\lib\site-packages (from pyasn1-modules>=0.2.1->google-auth>=1.4.1->google-api-python-client) (0.4.8)
from googleapiclient.discovery import build
import pandas as pd
import seaborn as sns
api_key = 'AIzaSyA5_lZVtLLZh28IQ8nNuz5GzHbFLAwppls'
channel_ids = ['UC8uU_wruBMHeeRma49dtZKA',
'UCJQJAI7IjbLcpsjWdSzYz0Q',
'UC7cs8q-gJRlGwj4A8OmCmXg',
'UCnz-ZXXER4jOvuED5trXfEA',
'UCq6XkhO5SZ66N04IcPbqNcw',
'UCJtUOos_MwJa_Ewii-R3cJA']
#code to start the service, you can get it at Youtube data API - References - Channels - list
youtube = build('youtube', 'v3', developerKey=api_key)
def get_channel_stats(youtube, channel_ids):
request = youtube.channels().list(
part = 'snippet, contentDetails, statistics', id=','.join(channel_ids))
response = request.execute()
all_data = []
for i in range(len(response['items'])):
data = dict(Channel_name = response['items'][i]['snippet']['title'],
Country = response['items'][i]['snippet']['country'],
Subscribers = response['items'][i]['statistics']['subscriberCount'],
Views = response['items'][i]['statistics']['viewCount'],
Total_videos = response['items'][i]['statistics']['videoCount'],
Playlist_id = response['items'][i]['contentDetails']['relatedPlaylists']['uploads'])
all_data.append(data)
return all_data
channel_data = get_channel_stats(youtube, channel_ids)
#creating a pandas dataframe to store the results
df = pd.DataFrame(channel_data)
df.head()
| Channel_name | Country | Subscribers | Views | Total_videos | Playlist_id | |
|---|---|---|---|---|---|---|
| 0 | Chandoo | NZ | 465000 | 36045811 | 425 | UU8uU_wruBMHeeRma49dtZKA |
| 1 | Keith Galli | US | 195000 | 13324613 | 78 | UUq6XkhO5SZ66N04IcPbqNcw |
| 2 | Alex The Analyst | US | 490000 | 19397419 | 215 | UU7cs8q-gJRlGwj4A8OmCmXg |
| 3 | Leila Gharani | AT | 2260000 | 206433028 | 537 | UUJtUOos_MwJa_Ewii-R3cJA |
| 4 | techTFQ | MY | 204000 | 10053097 | 89 | UUnz-ZXXER4jOvuED5trXfEA |
df.dtypes
Channel_name object Country object Subscribers object Views object Total_videos object Playlist_id object dtype: object
df['Subscribers'] = pd.to_numeric(df['Subscribers'])
df['Views'] = pd.to_numeric(df['Views'])
df['Total_videos'] = pd.to_numeric(df['Total_videos'])
df.dtypes
Channel_name object Country object Subscribers int64 Views int64 Total_videos int64 Playlist_id object dtype: object
sns.set(rc={'figure.figsize':(10,8)})
ax = sns.barplot(x='Channel_name', y='Subscribers', data=df, order=df.sort_values('Subscribers').Channel_name)
sns.set(rc={'figure.figsize':(10,8)})
ax = sns.barplot(x='Channel_name', y='Views', data=df)
sns.set(rc={'figure.figsize':(10,8)})
ax = sns.barplot(x='Channel_name', y='Total_videos', data=df)
playlist_id = df.loc[df['Channel_name'] == 'Thu Vu data analytics','Playlist_id'].iloc[0]
playlist_id
'UUJQJAI7IjbLcpsjWdSzYz0Q'
def get_video_id(youtube, playlist_id):
request = youtube.playlistItems().list(
part = 'contentDetails', playlistId = playlist_id,
maxResults = 50)
response = request.execute()
all_videos = []
for i in range(len(response['items'])):
all_videos.append(response['items'][i]['contentDetails']['videoId'])
next_page_token = response.get('nextPageToken')
more_pages = True
while more_pages:
if next_page_token is None:
more_pages = False
else:
request = youtube.playlistItems().list(
part = 'contentDetails', playlistId = playlist_id,
maxResults = 50,
pageToken = next_page_token)
response = request.execute()
for i in range(len(response['items'])):
all_videos.append(response['items'][i]['contentDetails']['videoId'])
next_page_token = response.get('nextPageToken')
return all_videos
video_ids = get_video_id(youtube, playlist_id)
def get_video_details (youtube, video_ids):
all_video_details = []
for i in range(0, len(video_ids), 50):
request = youtube.videos().list(
part = 'snippet, statistics',
id=','.join(Chandoo_video_ids[i:i+50]))
response = request.execute()
for video in response['items']:
video_details = dict(Title = video['snippet']['title'],
Published_Date = video['snippet']['publishedAt'],
Likes = video['statistics']['likeCount'],
Favourites = video['statistics']['favoriteCount'],
Views = video['statistics']['viewCount'],
Comments = video['statistics']['commentCount']
)
all_video_details.append(video_details)
return all_video_details
t_video_details = get_video_details (youtube, video_ids)
c_df = pd.DataFrame(t_video_details)
c_df
| Title | Published_Date | Likes | Favourites | Views | Comments | |
|---|---|---|---|---|---|---|
| 0 | How would I learn Excel today If I have to sta... | 2023-01-31T07:00:30Z | 783 | 0 | 16921 | 29 |
| 1 | Top 10 Advanced Excel Tricks for Data Analysis... | 2023-01-27T19:18:56Z | 1299 | 0 | 27878 | 39 |
| 2 | Create Org. Charts in Excel - Easy & Dynamic M... | 2023-01-24T07:00:23Z | 906 | 0 | 20350 | 36 |
| 3 | What! We don't need to learn DAX anymore? (New... | 2023-01-17T07:00:17Z | 3177 | 0 | 133894 | 142 |
| 4 | This is the ~ONLY~ Data Skill you need to FOCU... | 2023-01-10T07:00:07Z | 3325 | 0 | 106855 | 150 |
| ... | ... | ... | ... | ... | ... | ... |
| 95 | Data Analyst Job Interview - Don't make these ... | 2021-10-13T07:30:04Z | 1757 | 0 | 32988 | 111 |
| 96 | Excel has a new look - My first impressions | 2021-10-07T07:30:10Z | 522 | 0 | 41879 | 78 |
| 97 | How to use Microsoft Excel - Beginner to Inter... | 2021-10-05T08:10:06Z | 4492 | 0 | 170249 | 217 |
| 98 | I don’t use filters in Excel anymore. I use th... | 2021-09-30T07:30:01Z | 7899 | 0 | 433174 | 462 |
| 99 | How to create & use Drop Down lists like a PRO... | 2021-09-28T07:30:03Z | 742 | 0 | 32119 | 61 |
100 rows × 6 columns
c_df.dtypes
Title object Published_Date object Likes object Favourites object Views object Comments object dtype: object
c_df['Published_Date'] = pd.to_datetime(c_df['Published_Date']).dt.date
c_df['Likes'] = pd.to_numeric(c_df['Likes'])
c_df['Views'] = pd.to_numeric(c_df['Views'])
c_df['Comments'] = pd.to_numeric(c_df['Comments'])
c_df.dtypes
Title object Published_Date object Likes int64 Favourites object Views int64 Comments int64 dtype: object
c_df.head()
| Title | Published_Date | Likes | Favourites | Views | Comments | |
|---|---|---|---|---|---|---|
| 0 | How would I learn Excel today If I have to sta... | 2023-01-31 | 783 | 0 | 16921 | 29 |
| 1 | Top 10 Advanced Excel Tricks for Data Analysis... | 2023-01-27 | 1299 | 0 | 27878 | 39 |
| 2 | Create Org. Charts in Excel - Easy & Dynamic M... | 2023-01-24 | 906 | 0 | 20350 | 36 |
| 3 | What! We don't need to learn DAX anymore? (New... | 2023-01-17 | 3177 | 0 | 133894 | 142 |
| 4 | This is the ~ONLY~ Data Skill you need to FOCU... | 2023-01-10 | 3325 | 0 | 106855 | 150 |
top_10_videos = c_df.sort_values(by="Views", ascending=False).head(10)
top_10_videos
| Title | Published_Date | Likes | Favourites | Views | Comments | |
|---|---|---|---|---|---|---|
| 13 | Top 5 Excel Functions for Finance People (with... | 2022-10-27 | 1706 | 0 | 1464056 | 123 |
| 17 | I saw my boss do these 10 things in Excel! | 2022-10-04 | 46902 | 0 | 1441541 | 1055 |
| 38 | How I made $100K as Excel Freelancer - 4 life ... | 2022-06-21 | 10940 | 0 | 626079 | 348 |
| 50 | I don't use VLOOKUP anymore. I use this instea... | 2022-04-26 | 9253 | 0 | 570345 | 268 |
| 83 | AWESOME Excel trick to combine data from multi... | 2021-11-23 | 9273 | 0 | 555477 | 443 |
| 60 | BUSINESS Analyst vs. DATA Analyst - What do th... | 2022-03-10 | 14645 | 0 | 502713 | 412 |
| 98 | I don’t use filters in Excel anymore. I use th... | 2021-09-30 | 7899 | 0 | 433174 | 462 |
| 26 | Learn SQL for Data Analysis in one hour (with ... | 2022-08-23 | 14092 | 0 | 371291 | 473 |
| 44 | Learn Python - Getting started + 3 Projects fo... | 2022-05-24 | 7932 | 0 | 349725 | 255 |
| 92 | KNOW the difference between Data Base // Data ... | 2021-10-26 | 10889 | 0 | 348355 | 396 |
ax1 = sns.barplot(x='Views', y='Title', data=top_10_videos)
C:\Users\Mrs Muvhango\anaconda3\envs\YT-env\lib\site-packages\IPython\core\events.py:89: UserWarning: Glyph 128526 (\N{SMILING FACE WITH SUNGLASSES}) missing from current font.
func(*args, **kwargs)
C:\Users\Mrs Muvhango\anaconda3\envs\YT-env\lib\site-packages\IPython\core\events.py:89: UserWarning: Glyph 128076 (\N{OK HAND SIGN}) missing from current font.
func(*args, **kwargs)
C:\Users\Mrs Muvhango\anaconda3\envs\YT-env\lib\site-packages\IPython\core\pylabtools.py:152: UserWarning: Glyph 128526 (\N{SMILING FACE WITH SUNGLASSES}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
C:\Users\Mrs Muvhango\anaconda3\envs\YT-env\lib\site-packages\IPython\core\pylabtools.py:152: UserWarning: Glyph 128076 (\N{OK HAND SIGN}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
c_df
| Title | Published_Date | Likes | Favourites | Views | Comments | |
|---|---|---|---|---|---|---|
| 0 | How would I learn Excel today If I have to sta... | 2023-01-31 | 783 | 0 | 16921 | 29 |
| 1 | Top 10 Advanced Excel Tricks for Data Analysis... | 2023-01-27 | 1299 | 0 | 27878 | 39 |
| 2 | Create Org. Charts in Excel - Easy & Dynamic M... | 2023-01-24 | 906 | 0 | 20350 | 36 |
| 3 | What! We don't need to learn DAX anymore? (New... | 2023-01-17 | 3177 | 0 | 133894 | 142 |
| 4 | This is the ~ONLY~ Data Skill you need to FOCU... | 2023-01-10 | 3325 | 0 | 106855 | 150 |
| ... | ... | ... | ... | ... | ... | ... |
| 95 | Data Analyst Job Interview - Don't make these ... | 2021-10-13 | 1757 | 0 | 32988 | 111 |
| 96 | Excel has a new look - My first impressions | 2021-10-07 | 522 | 0 | 41879 | 78 |
| 97 | How to use Microsoft Excel - Beginner to Inter... | 2021-10-05 | 4492 | 0 | 170249 | 217 |
| 98 | I don’t use filters in Excel anymore. I use th... | 2021-09-30 | 7899 | 0 | 433174 | 462 |
| 99 | How to create & use Drop Down lists like a PRO... | 2021-09-28 | 742 | 0 | 32119 | 61 |
100 rows × 6 columns
c_df['month'] = pd.to_datetime(c_df['Published_Date']).dt.strftime('%b')
c_df
| Title | Published_Date | Likes | Favourites | Views | Comments | month | |
|---|---|---|---|---|---|---|---|
| 0 | How would I learn Excel today If I have to sta... | 2023-01-31 | 783 | 0 | 16921 | 29 | Jan |
| 1 | Top 10 Advanced Excel Tricks for Data Analysis... | 2023-01-27 | 1299 | 0 | 27878 | 39 | Jan |
| 2 | Create Org. Charts in Excel - Easy & Dynamic M... | 2023-01-24 | 906 | 0 | 20350 | 36 | Jan |
| 3 | What! We don't need to learn DAX anymore? (New... | 2023-01-17 | 3177 | 0 | 133894 | 142 | Jan |
| 4 | This is the ~ONLY~ Data Skill you need to FOCU... | 2023-01-10 | 3325 | 0 | 106855 | 150 | Jan |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 95 | Data Analyst Job Interview - Don't make these ... | 2021-10-13 | 1757 | 0 | 32988 | 111 | Oct |
| 96 | Excel has a new look - My first impressions | 2021-10-07 | 522 | 0 | 41879 | 78 | Oct |
| 97 | How to use Microsoft Excel - Beginner to Inter... | 2021-10-05 | 4492 | 0 | 170249 | 217 | Oct |
| 98 | I don’t use filters in Excel anymore. I use th... | 2021-09-30 | 7899 | 0 | 433174 | 462 | Sep |
| 99 | How to create & use Drop Down lists like a PRO... | 2021-09-28 | 742 | 0 | 32119 | 61 | Sep |
100 rows × 7 columns
uploads_per_month = c_df.groupby('month').size()
uploads_per_month
month Apr 6 Aug 6 Dec 9 Feb 5 Jan 12 Jul 5 Jun 7 Mar 8 May 8 Nov 14 Oct 12 Sep 8 dtype: int64
# converting the uploads_per_month variable to a dataframe
uploads_per_month = c_df.groupby('month', as_index=False).size()
uploads_per_month
| month | size | |
|---|---|---|
| 0 | Apr | 6 |
| 1 | Aug | 6 |
| 2 | Dec | 9 |
| 3 | Feb | 5 |
| 4 | Jan | 12 |
| 5 | Jul | 5 |
| 6 | Jun | 7 |
| 7 | Mar | 8 |
| 8 | May | 8 |
| 9 | Nov | 14 |
| 10 | Oct | 12 |
| 11 | Sep | 8 |
# using a categorical index to sort by months
sort_order = ['Jan','Feb','Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
# changing the numeric index into months
uploads_per_month.index = pd.CategoricalIndex(uploads_per_month['month'], categories = sort_order, ordered=True)
uploads_per_month = uploads_per_month.sort_index()
ax2 = sns.barplot(x='month', y='size', data=uploads_per_month)