{"cells":[{"metadata":{"_uuid":"b564c5bae2f2616d0af7cca413d5ffb9dd316659"},"cell_type":"markdown","source":"## 1. Loading Libraries and Data"},{"metadata":{"trusted":true,"_uuid":"fe5b6c203076f741e5b0768da393a4132bcd16d9"},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport time\nfrom datetime import datetime\n#import lightgbm as lgb\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.model_selection import KFold\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"818dbc618aebcac0b9d465874f7e5f1db1a87891"},"cell_type":"markdown","source":"Thanks ML for providing this load_df function and json code "},{"metadata":{"trusted":true,"_uuid":"4766bb2d59ade02a8c423f29bcc465f8b98c8aad"},"cell_type":"code","source":"import os\nimport json\nimport numpy as np\nimport pandas as pd\nfrom pandas.io.json import json_normalize\n\ndef load_df(csv_path='../input/train.csv', nrows=None):\n    JSON_COLUMNS = ['device', 'geoNetwork', 'totals', 'trafficSource']\n    \n    df = pd.read_csv(csv_path, \n                     converters={column: json.loads for column in JSON_COLUMNS}, \n                     dtype={'fullVisitorId': 'str'}, # Important!!\n                     nrows=nrows)\n    \n    for column in JSON_COLUMNS:\n        column_as_df = json_normalize(df[column])\n        column_as_df.columns = [f\"{column}.{subcolumn}\" for subcolumn in column_as_df.columns]\n        df = df.drop(column, axis=1).merge(column_as_df, right_index=True, left_index=True)\n    print(f\"Loaded {os.path.basename(csv_path)}. Shape: {df.shape}\")\n    return df\n\n#print(os.listdir(\"input\"))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7b035eb225676b6e5cda0e5fe1e5bbcaa26258d2"},"cell_type":"markdown","source":"loading the data file into train_df "},{"metadata":{"trusted":true,"_uuid":"adb0e22135d659e570ecb0a040ca524d962eecae"},"cell_type":"code","source":"%%time\ntrain_df = load_df()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"06616c154108940d9a16ba5c63392b63e3d921fb"},"cell_type":"markdown","source":"Display the list of columns in the dataset, and verify that all the JSON related columns have been properly added to the dataframe"},{"metadata":{"trusted":true,"_uuid":"ad48e38e357efa681637c15c13a7f12aa3751934"},"cell_type":"code","source":"train_df.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74dae1455797270ae993127d339278a99636c7cc"},"cell_type":"code","source":"# Let's check quickly how the data looks like after the conversion\n\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a1fb7bf6c6a6d944b2052e237292cd0fda0b4090"},"cell_type":"code","source":"#Temporarily take the dataframe into another variable and this sd variable is the one we will use it throughout the notebook \n\nsd = train_df","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"85e2410a1a91d15bdc3324ef5a25186a9e6e4749"},"cell_type":"markdown","source":"Let's see how many total columns are in the dataset and how many of them are null and not-null"},{"metadata":{"trusted":true,"_uuid":"1762d7affb6be6efed334ba5859390160345d6c4"},"cell_type":"code","source":"#let's check the info on all columns, how many null and not null values are and their associated data types\nsd.info()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"950b1f501c37d6803af764760154aaae5be4c0c9"},"cell_type":"markdown","source":"SessionId should be the unique identifier for each observation as it is a combination of fullVisitorId and visitId. The number of unique sessionId's comes close to the total number of observations (903,653), but is 898 short."},{"metadata":{"trusted":true,"_uuid":"b292b9268a52afe3aee3976a541f6688af1210a8"},"cell_type":"code","source":"sd.sessionId.nunique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6875ce8b49f4ec01223025dbba62a1c0f163144f"},"cell_type":"code","source":"#removing duploicate session Id from the dataframe\ndf = sd[sd.duplicated(subset='sessionId')]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b4e0207f6fd490612b70032b7d104e891c1c4edb"},"cell_type":"markdown","source":"Drop duplicates for sessionId fields"},{"metadata":{"trusted":true,"_uuid":"ffda419d6ea608d2229b34f862f20339ce0b98a4"},"cell_type":"code","source":"sd.drop_duplicates(subset='sessionId', keep=\"first\", inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e827e8e0863b22b096b3a3d09458eaa152e06334"},"cell_type":"code","source":"#new session id count after removing all duplicates\nsd.sessionId.count()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a74e6cf96ed6715dd234d702151832dc52ec2bef"},"cell_type":"code","source":"# Let's see how first record looks like\nsd.loc[0]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4700f0f9074f7421fae1b199bb2dedf04cdec8ac"},"cell_type":"markdown","source":"There are many columns which either have NULL values all over OR \"not available in demo dataset\". Which is not very useful to drive data analysis. Let;s convert the column to a list. and then use it for dropping those columns from dataframe"},{"metadata":{"trusted":true,"_uuid":"6ce7c37087e89071ad753610bdad575ba3db596c"},"cell_type":"code","source":"cols =     ['device.browserSize', 'device.browserVersion', 'device.language', 'device.mobileDeviceBranding',  'device.mobileDeviceInfo', \n            'device.mobileDeviceMarketingName', 'device.mobileDeviceModel', 'device.mobileInputSelector', 'device.operatingSystemVersion',\n            'device.screenColors', 'device.screenResolution', 'device.flashVersion','geoNetwork.cityId', 'geoNetwork.latitude', 'geoNetwork.longitude','geoNetwork.networkLocation',\n           'trafficSource.campaignCode']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ef1c03ced75d0c7cab56320133209fee2d001851"},"cell_type":"code","source":"# Drop unwanted columns from dataframe which does not have useful information (null values etc)\nsd.drop(columns=cols, axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"deabc504d5ac40023583f85abac463416c5982b3"},"cell_type":"code","source":"#Also there are many columns which have numeric values and currently they are Object, so let's convert them into numberic values\nsd[[ \n       'trafficSource.adwordsClickInfo.page',\n         'totals.bounces', 'totals.hits',\n       'totals.newVisits', 'totals.pageviews', 'totals.transactionRevenue', 'totals.visits']] = sd[[ \n       'trafficSource.adwordsClickInfo.page',\n         'totals.bounces', 'totals.hits',\n       'totals.newVisits', 'totals.pageviews', 'totals.transactionRevenue', 'totals.visits']].apply(pd.to_numeric)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ce8cf0dde97a6b536df5f4d0d603ea19e074e1c1"},"cell_type":"code","source":"# If you see the transaction amount, they are too high value\nsd['totals.transactionRevenue'].max()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a6fb8aabb742004140e7e2e54847ce477cd924b1"},"cell_type":"markdown","source":"#correcting transaction revenues (see https://www.kaggle.com/c/google-analytics-customer-revenue-prediction/discussion/65775). This is for EDA purposes only. In the competition, we need to predict the log of the tranaction values as they are stored in the dataset (so not divided by 1,000,000).\n"},{"metadata":{"trusted":true,"_uuid":"431b147018c6d61d5e962a9946eaeda8fbc8a4b9"},"cell_type":"code","source":"#fixing the transactionRevenue column data in dataframe\nsd['totals.transactionRevenue'] = sd['totals.transactionRevenue']/1000000","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0a918038d3892b94c569d73cef098258a9c999c8"},"cell_type":"code","source":"# Some useful stats around transactionRevenue count and transactionRevenue amount data\n\nprint(\"Total transactions count over 0 dollars \" +  sd[sd['totals.transactionRevenue'] > 0]['totals.transactionRevenue'].count().astype(str))\nprint(\"Total transactions amount over 0 dollars \" +  sd[sd['totals.transactionRevenue'] > 0]['totals.transactionRevenue'].sum().astype(str))\nprint(\"Minimum transactions amount over 0 dollars \" +  sd[sd['totals.transactionRevenue'] > 0]['totals.transactionRevenue'].min().astype(str))\nprint(\"Maximum transactions amount over 0 dollars \" +  sd[sd['totals.transactionRevenue'] > 0]['totals.transactionRevenue'].max().astype(str))\nprint(\"Total transactions count over 1000 dollars \" +  sd[sd['totals.transactionRevenue'] > 1000]['totals.transactionRevenue'].count().astype(str))\nprint(\"Total transactions amoount over 1000 dollars \" +  sd[sd['totals.transactionRevenue'] > 1000]['totals.transactionRevenue'].sum().astype(str))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d763f2df25c62e85ec88d746c9e5c2a88a54bdb6"},"cell_type":"markdown","source":"As the distribution of revenues is very right skewed, with the tail reaching 23,000 USD, I am below only displaying the histogram of the transaction with revenues below 1,000 USD."},{"metadata":{"trusted":true,"_uuid":"0b1163baf1d8057f602fc24fa52c830470b0bf7c"},"cell_type":"code","source":"sd[(sd['totals.transactionRevenue'] > 0) & (sd['totals.transactionRevenue'] < 1000)]['totals.transactionRevenue'].plot(kind='hist', bins=120, sort_columns=False, figsize=[14,7])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2548859858b103a9619e7f4e4f2130d7d1d097ce"},"cell_type":"markdown","source":"Converting date column from int64 to date data type"},{"metadata":{"trusted":true,"_uuid":"550c924b5c9d1d456c059f1c9d14e70d931cfdf8"},"cell_type":"code","source":"sd['date'] = pd.to_datetime(sd['date'].astype(str), format='%Y%m%d')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1a6e97bf57f1dd9e33f61c98a063ba22e2a40631"},"cell_type":"code","source":"# Showing a line graph with transaction revenue generated per day for the entire dataset between August 2017 - August 2018\n# It's clear that during the month of Oct-Nov-Dec 2017 the overall mean level of the transactions were higher than other months\n\nsd.groupby(['date'])['totals.transactionRevenue'].sum().plot(kind='line', sort_columns=False, figsize=[14,7], title='Daily Revenue (USD)', label='Transaction Revenue $ Amount', markevery=500, legend=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8e09583ae24ed963ea4ec97cfca301ae7c048028"},"cell_type":"code","source":"# Below graph shows # of transactions done by customers. It's visible that during Christmas time there were most number of transactions\n# and again during May 2018... could be because schools are off and people do shopping a lot before summer vacations ?? :) \nsd.groupby(['date'])['totals.transactionRevenue'].count().plot(kind='line', sort_columns=False, figsize=[14,7], title='Daily number of session', label='Session count', markevery=100, legend=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c65a3f99d467ffdcca529bfd9e92bfe469bd4391"},"cell_type":"code","source":"# Let's add a weekday name like sunday, monday, tuesday....saturday for further analysis\nsd['day_of_week'] = sd['date'].dt.weekday_name","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3c88724acef5b5ef32f28fed5e69d15ca815fdf9"},"cell_type":"code","source":"# Let's add day number for sorting purpose\nsd['day_number'] = sd['date'].dt.weekday","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5523b7c8d84c7c38a56343069b063495b853e68b"},"cell_type":"code","source":"# Let's see which day of the week store get's most revenue in a year. It seems Tuesday is the winner. Saturday/Sunday people dont shop much online it seems \n\nax = sd.groupby(['day_number'])['totals.transactionRevenue'].sum().plot(kind='bar', sort_columns=False, figsize=[14,7], title='Revenue per weekday (USD)', label='Transaction Revenue $ Amount',legend=True)\nax.set_xticklabels(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'])    \nax.set_ylabel('Revenue(USD)')\nax.set_xlabel('Day of the week')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d3e4126aacae99c5f2cc4d5ad1ff0259eabd8c6d"},"cell_type":"code","source":"# Most number of transactions are done on Monday and very low transaction rate is found on Saturday and Sunday\n\nax = sd.groupby(['day_number'])['totals.transactionRevenue'].count().plot(kind='bar', sort_columns=False, figsize=[14,7], title='Number of Session on weekday', label='Number of sessions',legend=True)\nax.set_xticklabels(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'])    \nax.set_ylabel('Number of Sessions')\nax.set_xlabel('Day of the week')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5fefdf419e539841e22f1743d3c94ee4e02b27cf"},"cell_type":"code","source":"# Now which month of the year get's most revenue... it turns out that April, August and December are the months\n# April = Summer vacation\n# August = School about to start\n# December = Christmas Vacations\nax = sd.groupby([sd['date'].dt.month])['totals.transactionRevenue'].sum().plot(kind='bar', width=.9, sort_columns=False, figsize=[14,7], title='Revenue per month', label='Revenue',legend=True)\nax.set_xticklabels(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])    \nax.set_ylabel('Revenue per month (USD)')\nax.set_xlabel('Months')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3e793c59e92f8ad2098d2ad86e701d8cedc1501a"},"cell_type":"code","source":"# most number of transactions are done in December\n\nax = sd.groupby([sd['date'].dt.month])['totals.transactionRevenue'].count().plot(kind='bar', width=.9, sort_columns=False, figsize=[14,7], title='Number of Session per month', label='Number of sessions',legend=True)\nax.set_xticklabels(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])    \nax.set_ylabel('Number of Sessions per month')\nax.set_xlabel('Months')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"92fc94eb81a4b51f77185a2ad959b60580620377"},"cell_type":"code","source":"# Transaction revenue per channel grouping\n\nax = sd.groupby([sd['channelGrouping']])['totals.transactionRevenue'].sum().plot(kind='bar', width=.9, sort_columns=True, figsize=[14,7], title='Revenue per Channel', label='Revenue',legend=True)\nax.set_ylabel('Revenue (USD)')\nax.set_xlabel('Channel Name')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cb7269ce9789207d21b8b787ef7793f3584ed51f"},"cell_type":"code","source":"# Most number of sessions per channel grouping\n\nax = sd['channelGrouping'].value_counts().plot(kind='bar', width=.9, sort_columns=True, figsize=[14,7], title='Number of Sessions per Channel', label='Number of sessions',legend=True)\nax.set_ylabel('Number of Sessions')\nax.set_xlabel('Channel Name')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"65b24ae1528a6dcbd1839b4aeed5b45c075a9e08"},"cell_type":"code","source":"# revenue generated by source\nsd.groupby([sd['trafficSource.source']])['totals.transactionRevenue'].sum().sort_values(ascending=False)[:20]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2e20eec4260f9c5c7aecaa2b724cec76ace50feb"},"cell_type":"code","source":"#Similarly plot for the revenue generated by source\n\nax = sd.groupby([sd['trafficSource.source']])['totals.transactionRevenue'].sum().sort_values(ascending=False)[:20].sort_values().plot(kind='barh', width=.9, sort_columns=True, figsize=[14,7], title='Revenue per Source', label='Revenue',legend=True)\nax.set_ylabel('Source Name')\nax.set_xlabel('Revenue (USD)')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"da3e2ddc55c827fde35e7c95c42a202f24e83692"},"cell_type":"code","source":"ax = sd['trafficSource.source'].value_counts()[:20].sort_values().plot(kind='barh', width=.9, sort_columns=False, figsize=[14,7], title='Number of Sessions per Source', label='Number of sessions',legend=True)\nax.set_ylabel('Source Name')\nax.set_xlabel('Number of Sessions')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"87a41c2444849c12d896c1eeb024b6356ce87f0b"},"cell_type":"code","source":"ax = sd.groupby([sd['device.deviceCategory']])['totals.transactionRevenue'].sum().sort_values(ascending=False)[:20].plot(kind='bar', width=.9, sort_columns=True, figsize=[14,7], title='Revenue per Device Category', label='Revenue',legend=True)\nax.set_xlabel('Device Category')\nax.set_ylabel('Revenue (USD)')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"42813357970beaa6888eaabbc531c157bb8d4738"},"cell_type":"code","source":"ax = sd['device.deviceCategory'].value_counts()[:20].plot(kind='bar', width=.9, sort_columns=False, figsize=[14,7], title='Number of Sessions per Device Category', label='Number of sessions',legend=True)\nax.set_xlabel('Device Category')\nax.set_ylabel('Number of Sessions')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"72416fc3174d5fe8c90172118ee474f320038537"},"cell_type":"code","source":"ax = sd.groupby([sd['device.operatingSystem']])['totals.transactionRevenue'].sum().sort_values(ascending=False)[:7].plot(kind='bar', width=.9, sort_columns=True, figsize=[14,7], title='Revenue per Operating System', label='Revenue',legend=True)\nax.set_xlabel('Operating System')\nax.set_ylabel('Revenue (USD)')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b6652a82a089ed3d462ce6a424e67f352a01f7f3"},"cell_type":"code","source":"ax = sd['device.operatingSystem'].value_counts()[:7].plot(kind='bar', width=.9, sort_columns=False, figsize=[14,7], title='Number of Sessions per Operating System', label='Number of sessions',legend=True)\nax.set_xlabel('Operating System')\nax.set_ylabel('Number of Sessions')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8f986a8842dc825795ae4ac6e370c52cb9926c7"},"cell_type":"code","source":"ax = sd.groupby([sd['device.browser']])['totals.transactionRevenue'].sum().sort_values(ascending=False)[:9].plot(kind='bar', width=.9, sort_columns=True, figsize=[14,7], title='Revenue per Browser', label='Revenue',legend=True)\nax.set_xlabel('Browser')\nax.set_ylabel('Revenue (USD)')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b19d654782699dd91d7a3f87fcb216fe01e07b1a"},"cell_type":"code","source":"ax = sd['device.browser'].value_counts()[:9].plot(kind='bar', width=.9, sort_columns=False, figsize=[14,7], title='Number of Sessions per Browser', label='Number of sessions',legend=True)\nax.set_xlabel('Browser')\nax.set_ylabel('Number of Sessions')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b36049cb955c445cf39359b641adc51154d1cf47"},"cell_type":"code","source":"ax = sd.groupby([sd['totals.pageviews']])['totals.transactionRevenue'].sum()[:75].plot(kind='bar', width=.9, sort_columns=True, figsize=[14,7], title='Revenue Pageviews', label='Revenue',legend=True)\nax.set_xlabel('Pageviews')\nax.set_ylabel('Revenue (USD)')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a4d803968aee263e930e99b4ddb4c19d1b6656c8"},"cell_type":"code","source":"ax = sd['totals.pageviews'].value_counts()[:30].plot(kind='bar', width=.9, sort_columns=False, figsize=[14,7], title='Number of Sessions per pageviews', label='Number of sessions',legend=True)\nax.set_xlabel('Pageviews')\nax.set_ylabel('Number of Sessions')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a15f35719337bb82fa2ac580c5e02b83f01ecbef"},"cell_type":"code","source":"ax = sd[(sd['totals.pageviews'] <= 100) & (sd['totals.transactionRevenue'] > 0)]['totals.pageviews'].value_counts().sort_values(ascending=False)[:50].plot(kind='bar', width=.9, sort_columns=True, figsize=[14,7], title='Number of Sessions per pageviews', label='Number of sessions',legend=True)\nax.set_xlabel('Pageviews')\nax.set_ylabel('Number of Sessions with transaction revenue')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f553189620606f7ebdb262ad8762a429efffd38e"},"cell_type":"code","source":"print('Total number of bounces ' + sd[(sd['totals.bounces'] == 1)].count().iloc[1].astype(str))\nprint('Total revenue (USD) generated from bounces ' + sd[(sd['totals.bounces'] == 1) & (sd['totals.transactionRevenue'] > 0)]['totals.transactionRevenue'].sum().astype(str))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"24697069167df1430fa21ec8ba01f08bad8ac6f3"},"cell_type":"code","source":"print('Total number of single pageviews ' + sd[(sd['totals.pageviews'] == 1)].count().iloc[1].astype(str))\nprint('Total revenue (USD) generated from single pageviews ' + sd[(sd['totals.pageviews'] == 1) & (sd['totals.transactionRevenue'] > 0)]['totals.transactionRevenue'].sum().astype(str))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d9e6bc589ccb4d2894298c107ccab733928c81b6"},"cell_type":"code","source":"ax = sd.groupby([sd[(sd['totals.hits'] <200 )]['totals.hits']])['totals.transactionRevenue'].sum()[:100].plot(kind='bar', width=.9, sort_columns=True, figsize=[14,7], title='Revenue page hits', label='Revenue',legend=True)\nax.set_xlabel('Pagehits')\nax.set_ylabel('Revenue (USD)')\nax.set_xmargin(.5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"caa7b5553d0504b8f1a978535b07deef9b2e1907"},"cell_type":"code","source":"print(sd.groupby([sd[(sd['totals.transactionRevenue'] > 1 )]['geoNetwork.country']])['totals.transactionRevenue'].sum().sort_values(ascending=False)[:10])\nax = sd.groupby([sd[(sd['totals.transactionRevenue'] > 1 )]['geoNetwork.country']])['totals.transactionRevenue'].sum().sort_values(ascending=False)[:10].plot(kind='bar', width=.9, sort_columns=True, figsize=[14,7], title='Revenue per country', label='Revenue',legend=True)\nax.set_xlabel('Country')\nax.set_ylabel('Revenue (USD)')\nax.set_xmargin(.5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f16b66638cf861ff378f1397586a00df1c5f9c6"},"cell_type":"code","source":"print('All visits by Country  - top 10')\nprint(sd.groupby(['geoNetwork.country'])['totals.visits'].sum().sort_values(ascending=False)[:10])\nprint('\\nAll Revenue making visits by Country - top 10')\nprint(sd.groupby([sd[(sd['totals.transactionRevenue'] > 1 )]['geoNetwork.country']])['totals.visits'].sum().sort_values(ascending=False)[:10])\n\nsd.groupby(['geoNetwork.country'])['totals.visits'].sum().sort_values(ascending=False)[:10].plot(kind='line', sort_columns=True, figsize=[14,7], label='Total Visits (right)',legend=True, subplots=True, sharey=False, secondary_y=True, colormap='rainbow')\nax = sd.groupby([sd[(sd['totals.transactionRevenue'] > 1 )]['geoNetwork.country']])['totals.visits'].sum().sort_values(ascending=False)[:10].plot(kind='bar', width=.9, sort_columns=True, figsize=[14,7], title='Revenue per country', label='Revenue making visits (left)',legend=True)\nax.set_xlabel('Country')\nax.set_ylabel('Revenue (USD)')\nax.set_xmargin(.5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1d7ef5994cab960066a3bd28afde29fa5c8d5eac"},"cell_type":"code","source":"print('\\nMean Transaction Revenue by Country where Transaction Revenue is > $0 - top 10')\nprint(sd.groupby([sd[(sd['totals.transactionRevenue'] > 0 )]['geoNetwork.country']])['totals.transactionRevenue'].mean().sort_values(ascending=False)[:10])\n\nax = sd.groupby([sd[(sd['totals.transactionRevenue'] > 0 )]['geoNetwork.country']])['totals.transactionRevenue'].mean().sort_values(ascending=False)[:10].plot(kind='bar', width=.9, sort_columns=True, figsize=[14,7], title='Mean Revenue per country with transaction amount > $0', label='Mean Revenue',legend=True)\nax.set_xlabel('Country')\nax.set_ylabel('Revenue (USD)')\nax.set_xmargin(.5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1e3a79683fdcae9e864b00fb0136e0cf42fb1200"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"86fbbc6aca7877a594ba0a7f7ea726223ab6865f"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}