Google Analytics Exploration -1
Insert the link to your Google Sheet
import pandas as pd
import re
# This function will convert the url to a download link
def convert_gsheets_url(u):
try:
worksheet_id = u.split('#gid=')[1]
except:
# Couldn't get worksheet id. Ignore it
worksheet_id = None
u = re.findall('https://docs.google.com/spreadsheets/d/.*?/',u)[0]
u += 'export'
u += '?format=csv'
if worksheet_id:
u += '&gid={}'.format(worksheet_id)
return u
sample_url = 'https://docs.google.com/spreadsheets/d/1ih4V4CumuIl5ZynobsazNzGiaPrE2V2Dpt13FI22XNU/edit'
try:
url = convert_gsheets_url(URL)
df = pd.read_csv(url)
print('Read successfully')
except:
print(f"Could not read any data from the URL you provided.\nReading from {sample_url} instead.")
url = convert_gsheets_url(sample_url)
df = pd.read_csv(url)
Could not read any data from the URL you provided.
Reading from https://docs.google.com/spreadsheets/d/1ih4V4CumuIl5ZynobsazNzGiaPrE2V2Dpt13FI22XNU/edit instead.
Exploring the data types
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 54 entries, 0 to 53
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 president_number 54 non-null int64
1 name 54 non-null object
2 birth 54 non-null object
3 death 48 non-null object
4 tenure_start 54 non-null object
5 tenure_end 53 non-null object
6 president 54 non-null object
7 president_relationship 54 non-null object
dtypes: int64(1), object(7)
memory usage: 3.5+ KB
The top 10 pages on the website: An overview
Visualization of top 10 pages by "page views"
General Stats for the top 10 pages
df.describe()
Identifying the best performing pages on the website
df = df.astype({"Bounce Rate": str})
df['Bounce Rate'] = df['Bounce Rate'].str.rstrip('%').astype('float')
df.info()
best_performing = df[(df["Page Views"]>= 20) & (df["Bounce Rate"]<= 0.9)]
print(best_performing)
FLOAT---------
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 7 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Page 10 non-null object
1 Page Views 10 non-null int64
2 Unique Page Views 10 non-null int64
3 Avg. Time on Page 10 non-null object
4 Entrances 10 non-null int64
5 Bounce Rate 10 non-null float64
6 % Exit 10 non-null object
dtypes: float64(1), int64(3), object(3)
memory usage: 688.0+ bytes
Page Page Views \
0 / 267
1 /graphic-design-agency/swift-india-conf-2019 163
2 /contact 77
3 /graphic-design-agency 60
4 /illustration-design 39
5 /ui-ux-user-experience-design 37
6 /digital-marketing-services 34
7 /brands-spaces 33
8 /cart 30
9 /editorial-documentary-photography 29
Unique Page Views Avg. Time on Page Entrances Bounce Rate % Exit
0 229 00:01:53 225 0.007111 67.79%
1 163 00:04:06 156 0.010000 96.93%
2 75 00:00:11 60 0.010000 85.71%
3 42 00:00:48 15 0.004667 23.33%
4 28 00:00:54 13 0.006154 35.90%
5 32 00:00:39 6 0.008333 24.32%
6 27 00:00:12 7 0.007143 41.18%
7 26 00:02:22 8 0.008750 30.30%
8 30 00:00:00 30 0.010000 100.00%
9 25 00:00:10 6 0.008333 31.03%
best_performing