{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"markdown","source":"<img src=\"https://www.amazeemetrics.com/sites/default/files/Getting-Started-with-Google-Analytics.jpg\">"},{"metadata":{"_uuid":"213804b7cdf65ace2e3fbab2732282cff78ebdb3"},"cell_type":"markdown","source":"<a href='#'>Preface</a><br>\n<a href='#desc'>description</a><br>\n<a href='#about'>About notebook</a><br>\n<a href='#load_lib'>Load libraries</a><br>\n<a href='#load_data'>Load Dataset</a><br>\n<a href='#description'>Column Description</a><br>\n<a href='#cleaning'>Cleaning Dataset</a><br>\n<a href='#eda'>EDA</a><br>\n- <a href='#null'>Null values</a><br>\n- <a href='#channel'>Via which channel did user visited </a><br>\n- <a href='#mobile'>Mobile users</a><br>\n- <a href='#browser'>Browser based </a><br>\n- <a href='#device'>Device Category</a><br>\n- <a href='#os'>Operating system</a><br>\n- <a href='#continent'>Continent Based</a><br>\n- <a href='#metro'>Metro Based</a><br>\n- <a href='#networkdomain'>Network Domain</a><br>\n- <a href='#region'>Region</a><br>\n- <a href='#country'>Country Based</a><br>\n- <a href='#subcountry'>Sub Continent Based</a><br>\n- <a href='#pVb'>Page view V/S bounces</a><br>\n- <a href='#customers'>new cusotmer or old customer</a><br>\n- <a href='#minmax'>Minmum & maximum revenu on daily basis</a><br>\n- <a href='#month'>Revenue based on month</a><br>\n- <a href='#day'>Revenue based on day</a><br>\n- <a href='#weekday'>Revenue based on weekday</a><br>\n- <a href='#adcontent'>Most Ad Content</a><br>\n- <a href='#keywords'>Keywords used by users</a><br>\n- <a href='#source'>Source from where users came</a><br>"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"markdown","source":"# <a id='desc'> Description</a>"},{"metadata":{"_uuid":"6a5ca5ac3ebe250a6037ead4c9a3b1a419e501cf"},"cell_type":"markdown","source":"In this competition, you’re challenged to analyze a Google Merchandise Store (also known as GStore, where Google swag is sold) customer dataset to predict revenue per customer. "},{"metadata":{"_uuid":"47bdb5fa6a9ec3e2085ed0edabe2e966d57408f4"},"cell_type":"markdown","source":"# <a id='about'>About notebook</a>"},{"metadata":{"_uuid":"7b1bf33eac082619e8b1bba817492ab64f3f01d6"},"cell_type":"markdown","source":"In this notebook we will look into the Dataset provided in the competetion and we will analyze the users of GStore."},{"metadata":{"_uuid":"d90f558040c32599cc0edee9cebe99c609ce779e"},"cell_type":"markdown","source":"# <a id='load_lib'>Load libraries</a>"},{"metadata":{"trusted":true,"_uuid":"e370c288693ababc3906167bca9d49f9fcdd2852","_kg_hide-input":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport plotly.graph_objs as go\nimport plotly.offline as py\nfrom plotly.offline import init_notebook_mode, iplot, download_plotlyjs\nimport plotly.graph_objs as go\nfrom plotly import tools\nimport matplotlib.pyplot as plt\ninit_notebook_mode(connected=True)\nfrom plotly.tools import FigureFactory as ff\nimport random\nfrom collections import Counter\nimport warnings\nimport json\nimport os\nimport datetime\nfrom pandas.io.json import json_normalize\nwarnings.filterwarnings('ignore')\npd.set_option('display.max_columns', 500)\nimport pycountry\nfrom wordcloud import WordCloud\nimport matplotlib.pyplot as plt\nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4203f368219b17f856ff64a0969ccd8981b8440f"},"cell_type":"markdown","source":"# <a id='load_data'>Load Dataset</a>"},{"metadata":{"trusted":true,"_uuid":"4fe0f609d240100c7ac9fb934500d0daaef31874","_kg_hide-input":true},"cell_type":"code","source":"train = pd.read_csv(\"../input/train.csv\")\ntest = pd.read_csv(\"../input/test.csv\")\n# train_df = pd.read_csv('flatten_train.csv')\n# test_df = pd.read_csv('flatten_test.csv')","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"9d6c957c80612dabe1fabfddf4ff1bc0565408ab"},"cell_type":"code","source":"# helper functions\ndef constant_cols(df):\n    cols = []\n    columns = df.columns.values\n    for col in columns:\n        if df[col].nunique(dropna = False) == 1:\n            cols.append(col)\n    return cols\n\ndef diff_cols(df1,df2):\n    columns1 = df1.columns.values\n    columns2 = df2.columns.values\n    print(list(set(columns1) - set(columns2)))\n    \n\ndef count_mean(col,color1,color2):\n    col_count = train_df[col].value_counts()\n    col_count_chart = go.Bar(x = col_count.head(10).index, y = col_count.head(10).values, name=\"Count\",marker = dict(color=color1))\n\n    col_mean_count = train_df[[col,'totals.transactionRevenue']][(train_df['totals.transactionRevenue'] >1)]\n    col_mean_count = col_mean_count.groupby(col)['totals.transactionRevenue'].mean().sort_values(ascending=False)\n    col_mean_count_chart = go.Bar(x = col_mean_count.head(10).index, y = col_mean_count.head(10).values, name=\"Mean\",marker = dict(color=color2))\n\n    fig = tools.make_subplots(rows = 1, cols = 2,subplot_titles=('Total Count','Mean Revenue'))\n    fig.append_trace(col_count_chart, 1,1)\n    fig.append_trace(col_mean_count_chart,1,2)\n    py.iplot(fig)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6c4d99e4443f69b38eb550f178e4be484ef392a7","_kg_hide-input":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1aeab0e92575a62665e84155bb484a79751bfecf"},"cell_type":"markdown","source":"# <a id='description'>Column Description</a><br>"},{"metadata":{"_uuid":"50e8f0408e8846a658833af1a4c7503e219e6184"},"cell_type":"markdown","source":"- fullVisitorId- A unique identifier for each user of the Google Merchandise Store.\n- channelGrouping - The channel via which the user came to the Store.\n- date - The date on which the user visited the Store.\n- device - The specifications for the device used to access the Store.\n- geoNetwork - This section contains information about the geography of the user.\n- sessionId - A unique identifier for this visit to the store.\n- socialEngagementType - Engagement type, either \"Socially Engaged\" or \"Not Socially Engaged\".\n- totals - This section contains aggregate values across the session.\n- trafficSource - This section contains information about the Traffic Source from which the session originated.\n- visitId - An identifier for this session. This is part of the value usually stored as the _utmb cookie. This is only unique to the user. For a completely unique ID, you should use a combination of fullVisitorId and visitId.\n- visitNumber - The session number for this user. If this is the first session, then this is set to 1.\n-visitStartTime - The timestamp (expressed as POSIX time)"},{"metadata":{"_uuid":"1a71258c17cba3bf97e229b9fb9e1235ca13559e"},"cell_type":"markdown","source":"Since few columns have json values lets convert flatten them"},{"metadata":{"_uuid":"c1ce34eb0d0600213879659dab5d7ca12cfd7c4f"},"cell_type":"markdown","source":"Source from where i got the code to flatten the json columns<br>\nhttps://www.kaggle.com/julian3833/1-quick-start-read-csv-and-flatten-json-fields/notebook"},{"metadata":{"trusted":true,"_uuid":"e5c96786d27c8bd63fda892d272722400c760bff","_kg_hide-input":true},"cell_type":"code","source":"def 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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5dfbb2a9a066364fb1c23a6a3cbdb639b591da1f","_kg_hide-input":true},"cell_type":"code","source":"train_df = load_df()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9c3c7ad84fee2a427836edd295ba28576b9f6e91","_kg_hide-input":true},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b7c019e456d18408d4b235799e9f2881eb02bd40","_kg_hide-input":true,"scrolled":true},"cell_type":"code","source":"test_df = load_df(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"dac8afd1751027c2e39c189ddede47fa5d6d8225","scrolled":true},"cell_type":"code","source":"# train_df.to_csv('flatten_train.csv')\n# test_df.to_csv('flatten_test.csv')\ndiff_cols(train_df,test_df)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"39175ad40622cd7ad6e253ed6bb4aff29c45c75d"},"cell_type":"markdown","source":"Since totals transaction Revenue is what we are going to predict.\nand there is no campaignCode in test set"},{"metadata":{"_uuid":"98e96b37a1f494c4dddd234033bd3e02270b91fb"},"cell_type":"markdown","source":"# <a id='cleaning'>Cleaning Dataset</a>"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"d67104b148fdd6717e92cf25f860221b243d2a7f"},"cell_type":"code","source":"train_constants = constant_cols(train_df)\ntest_constants = constant_cols(test_df)\nprint(train_constants)\nprint(test_constants)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9cb4b404b638c5f2dbf25a56aa0bb4dfbd9fb217","_kg_hide-input":true},"cell_type":"code","source":"train_df[\"totals.transactionRevenue\"] = train_df[\"totals.transactionRevenue\"].astype('float')\ntrain_df['totals.transactionRevenue'] = train_df['totals.transactionRevenue'].fillna(0)\ntrain_df['date'] = train_df['date'].astype(str)\ntrain_df[\"date\"] = train_df[\"date\"].apply(lambda x : x[:4] + \"-\" + x[4:6] + \"-\" + x[6:])\ntrain_df[\"date\"] = pd.to_datetime(train_df[\"date\"])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6381de141564396e4d00c0e7b8ce331db933389e"},"cell_type":"markdown","source":"## both the df has same cols with constant values lets remove them "},{"metadata":{"trusted":true,"_uuid":"cbdd8bdb206a0f72dab51ed5b8c2fc4829df6f69","_kg_hide-input":true},"cell_type":"code","source":"train_constants = constant_cols(train_df)\ntest_constants = constant_cols(test_df)\ntrain_df = train_df.drop(columns=train_constants,axis = 1)\ntest_df = test_df.drop(columns=test_constants, axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6ce216e0d8ea549e9a91d50f1fc5b969d22c98f5"},"cell_type":"markdown","source":"# <a id='eda'>EDA</a><br>"},{"metadata":{"_uuid":"e32508fcfa1586b071c54ebc39a860ffb6951147"},"cell_type":"markdown","source":"# <a id='null'>Null values</a><br>"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"94030147902c1bc296abdadf282bcef13c065999"},"cell_type":"code","source":"null_values = train_df.isna().sum(axis = 0).reset_index()\nnull_values = null_values[null_values[0] > 50]\nnull_chart = [go.Bar(y = null_values['index'],x = null_values[0]*100/len(train_df), orientation = 'h')]\npy.iplot(null_chart)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"74c85d2c20fff836c6cb906e19c3d2dbecf03cb3"},"cell_type":"markdown","source":"**Summary**<br>\n- So many coloumns has null values<br>\n- we will find why these columns are null and we will also see how we can manage them."},{"metadata":{"_uuid":"97bc8a6389a5d74fda80f2532a62c4b69d6c4eab"},"cell_type":"markdown","source":"# <a id='channel'>Via which channel did user visited </a><br>"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"6840fdcd33608a6aad6242b961bd4ca16a67037c"},"cell_type":"code","source":"data = train_df[['channelGrouping','totals.transactionRevenue']]\ntemp = data['channelGrouping'].value_counts()\nchart = [go.Pie(labels = temp.index, values = temp.values)]\npy.iplot(chart)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"acaa382be47c525c59b6f020164d6fb98c231203"},"cell_type":"markdown","source":"**Summary**<br>\n-  Most of the users came via organic search.<br>\n-   Paid search and affilate users are very less."},{"metadata":{"_uuid":"2c5426d903dc2352daaacc2866b6fc915c3bcf89"},"cell_type":"markdown","source":"# <a id='mobile'>Mobile users</a><br>"},{"metadata":{"trusted":true,"_uuid":"ac93900cd33a7d2fc01b29d54b201796f70f5a5d","_kg_hide-input":true},"cell_type":"code","source":"temp = train_df['device.isMobile'].value_counts()\nchart = go.Bar(x = [\"False\",\"True\"], y = temp.values)\npy.iplot([chart])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"df8725e72fa448b7348c110b5864c81dda04ce35"},"cell_type":"markdown","source":"**Summary**\n- Many users browse the site from desktop or tablet"},{"metadata":{"_uuid":"cde2c4e2fa7842d30acf92bb20bbd328a7272d41"},"cell_type":"markdown","source":"# <a id='browser'>Browser based</a><br>"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"b73b219f3e826d1777df2309d3cf51eed3b2c914"},"cell_type":"code","source":"count_mean('device.browser',\"#7FDBFF\",\"#3D9970\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f5a440a6b711acaf7a545fd755fefe1685f442c4"},"cell_type":"markdown","source":"**Summary**\n- The user visit count is very high for Chrome but Firefox users have genereted more revenue"},{"metadata":{"_uuid":"b618e9a84e509ee02da36b57ce712c8236c7207f"},"cell_type":"markdown","source":"# <a id='device'>Device Category</a><br>"},{"metadata":{"trusted":true,"_uuid":"2a14bbe3962a0b848e6229f0828f5b52985aca18","_kg_hide-input":true},"cell_type":"code","source":"count_mean('device.deviceCategory',\"#FF851B\",\"#FF4136\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5eae85dfa702b5f5ac463e20753d5ec320a0962b"},"cell_type":"markdown","source":"**Summary**\n- Desktop site has generated more user count as well as more revenue\n- One thing to note is tablet users have generated almost same revenue as mobile users "},{"metadata":{"trusted":true,"_uuid":"1281f037aaddfefddc600a08cfeb9f4735519739"},"cell_type":"markdown","source":"# <a id='os'>Operating system</a><br>"},{"metadata":{"trusted":true,"_uuid":"4c87cc5e40e6f121236928872e1dc2186ab0e13a","_kg_hide-input":true},"cell_type":"code","source":"count_mean('device.operatingSystem',\"#80DEEA\",\"#0097A7\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"eb3fd69f446aa87d27212a9c8aac44cfee5aac61"},"cell_type":"markdown","source":"**Summary**\n- Less chrome OS users but high revenue is generated.\n-For windows phone users also generated good revenue."},{"metadata":{"_uuid":"93108e424fa620995b2eda245a56c53066ea5d85"},"cell_type":"markdown","source":"# <a id='continent'>Continent Based</a><br>"},{"metadata":{"trusted":true,"_uuid":"ecb5a4ef113b05e7e94b592a80d3ab6eb66ffd6d","_kg_hide-input":true},"cell_type":"code","source":"count_mean('geoNetwork.continent',\"#F48FB1\",\"#C2185B\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ca05ef0b821f7135261718fa846af8737040d174"},"cell_type":"markdown","source":"**Summary**\n- African Users have generated more than a billion mean revenue.\n- Users from american have used the website a lot but didnt purchased products."},{"metadata":{"_uuid":"3129391bb56d73d7e16bfb74d41e0c4891d574c9"},"cell_type":"markdown","source":"# <a id='country'>Country Based</a><br>"},{"metadata":{"trusted":true,"_uuid":"c0986acf7acbfe1afeed8adc5f7ff555337875ee","_kg_hide-input":true},"cell_type":"code","source":"data = train_df[['geoNetwork.country','totals.transactionRevenue']][(train_df['totals.transactionRevenue'] >1)]\ntemp = data.groupby('geoNetwork.country',as_index=False)['totals.transactionRevenue'].mean()\ntemp['code'] = 'sample'\nfor i,country in enumerate(temp['geoNetwork.country']):\n    mapping = {country.name: country.alpha_3 for country in pycountry.countries}\n    temp.set_value(i,'code',mapping.get(country))\nchart = [ dict(\n        type = 'choropleth',\n        locations = temp['code'],\n        z = temp['totals.transactionRevenue'],\n        text = temp['geoNetwork.country'],\n        autocolorscale = True,\n        reversescale = True,\n        marker = dict(\n            line = dict (\n                color = 'rgb(180,180,180)',\n                  width = 0.5\n            ) ),\n        colorbar = dict(\n            autotick = True,\n            title = 'Mean Revenue'),\n      ) ]\n\nlayout = dict(\n    title = 'Mean revenue based on country',\n    geo = dict(\n        showframe = True,\n        showcoastlines = True,\n         showocean = True,\n        projection = dict(\n            type = 'Mercator'\n        )\n    )\n)\n\nfig = dict( data=chart, layout=layout )\npy.iplot( fig, validate=False)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b070a2b023785fec2e337e493949dc36169daa7c"},"cell_type":"markdown","source":"**Summary**\n- We can see the revenue generated based on the countries\n- only 4 countries in africa have generated huge mean revenue."},{"metadata":{"_uuid":"0131ebf7f3595d0464178f86785c609426f882f2"},"cell_type":"markdown","source":"# <a id='metro'>Metro Based</a><br>"},{"metadata":{"trusted":true,"_uuid":"29a1c69373cc06bcf16e62cbfee2f7501c3599c4","_kg_hide-input":true},"cell_type":"code","source":"count_mean('geoNetwork.metro',\"#CE93D8\", \"#7B1FA2\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4251e381456ffa8de2aeb355ca8796333108d673"},"cell_type":"markdown","source":"**Summary**\n- Most of the users metro location is not present in the dataset.\n- may be we have to assume some hypothesis here"},{"metadata":{"_uuid":"be2c01abe5b9ff538cce61bf40e833e9b4d6971e"},"cell_type":"markdown","source":"# <a id='networkdomain'>Network Domain</a><br>"},{"metadata":{"trusted":true,"_uuid":"6af3b2fba5fee8e7c5a069be916e6f5e20eb08b8","_kg_hide-input":true},"cell_type":"code","source":"count_mean('geoNetwork.networkDomain','#90CAF9','#1976D2')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1d7e134266bfc9440f5078ed32e40411aac8b7aa"},"cell_type":"markdown","source":"**Summary**\n- Most users who generate revenue uses digitalwest.net\n- Again same we have huge users count from unknown source/ (not set)"},{"metadata":{"_uuid":"9127fe25834c8c54188d8a8d1103870f116d011d"},"cell_type":"markdown","source":"# <a id='region'>Region</a><br>"},{"metadata":{"trusted":true,"_uuid":"981b7a1674eacb03600db6edc1e6b83c0e7597ac","_kg_hide-input":true},"cell_type":"code","source":"count_mean('geoNetwork.region','#DCE775','#AFB42B')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"517438287fd3361e8baecb851b1bc565ff246242"},"cell_type":"markdown","source":"** Summary**\n- Tokyo has generated more than 1 Billion mean revenue"},{"metadata":{"trusted":true,"_uuid":"a979cdbe0c487a49d9ac83c0128d95c36f0bc94a"},"cell_type":"markdown","source":"# <a id='subcountry'>Sub Continent Based</a><br>"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"7e775b9e6439fcff6f7b670b7d3f5f0fa3f4dd98"},"cell_type":"code","source":"count_mean('geoNetwork.subContinent','#FFE082','#FFA000')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"fd46b4e7b8d722aa6c935f0a3ab39609059b346c"},"cell_type":"markdown","source":"**Summary**\n- No surprise Africa stands top in mean revenue "},{"metadata":{"_uuid":"bb632f29e16fee3ef7c76eafa36832fcb390372a"},"cell_type":"markdown","source":"# <a id='pVb'>Page view V/S bounces</a><br>"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"664f96a103e4a7e588b6328d72588ec1b3f53a3a"},"cell_type":"code","source":"train_df['totals.pageviews'] = train_df['totals.pageviews'].fillna(0).astype('int32')\ntrain_df['totals.bounces'] = train_df['totals.bounces'].fillna(0).astype('int32')\n\npageview = train_df.groupby('date')['totals.pageviews'].apply(lambda x:x[x >= 1].count()).reset_index()\nbounce = train_df.groupby('date')['totals.bounces'].apply(lambda x:x[x >= 1].count()).reset_index()\n\npageviews = go.Scatter(x = pageview['date'],y= pageview['totals.pageviews'], name = 'Pageview',marker=dict(color = \"#B0BEC5\"))\n\n\nbounces = go.Scatter(x = bounce['date'],y= bounce['totals.bounces'],name = 'Bounce',marker=dict(color = \"#37474F\"))\n\npy.iplot([pageviews,bounces])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d8cbbf7a58f5e49f9e1abbabef170307d7ff0b9b"},"cell_type":"markdown","source":"**Summary**\n- We can see the based on pageview we have increase/decrease of bounce rate "},{"metadata":{"_uuid":"98632bd3d7d0a37bf015f6c81fa90e02dffb310e"},"cell_type":"markdown","source":"# <a id='customers'>new cusotmer or old customer</a><br>"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"e15212cacf88c0e1c61f9889532f0628c78d6945"},"cell_type":"code","source":"train_df['totals.newVisits'] = train_df['totals.newVisits'].fillna(0).astype('int32')\ntrain_df['totals.hits'] = train_df['totals.hits'].fillna(0).astype('int32')\n\nnewvisit = train_df.groupby('date')['totals.newVisits'].apply(lambda x:x[x == 1].count()).reset_index()\noldVisit = train_df.groupby('date')['totals.newVisits'].apply(lambda x:x[x == 0].count()).reset_index()\nhit = train_df.groupby('date')['totals.hits'].apply(lambda x:x[x >= 1].count()).reset_index()\n\n\nhits = go.Scatter(x = hit['date'],y = hit['totals.hits'], name = 'total hits', marker=dict(color = '#FFEE58'))\n\nnew_vist = go.Scatter(x = newvisit['date'],y= newvisit['totals.newVisits'],name = 'New Vists', marker=dict(color = '#F57F17'))\n\noldvisit = go.Scatter(x = oldVisit['date'],y = oldVisit['totals.newVisits'], name = 'Old Visit', marker=dict(color = '#FFD600'))\n\npy.iplot([hits, new_vist, oldvisit])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c642bbcf25406bc5010bc2c315f6d4477c10ef15"},"cell_type":"markdown","source":"** Summary **\n- Out of all the hits we have more new visit than old visit\n- That means the returning customer is very less than the new customers.\n- Or there can be other meaning."},{"metadata":{"trusted":true,"_uuid":"19ef5c256eb8b2c8d034ba26e6a88f6182dd3c2e"},"cell_type":"markdown","source":"# <a id='minmax'>Minmum & maximum revenue on daily basis</a><br>"},{"metadata":{"trusted":true,"_uuid":"5e249d6f5381156281feb1b490ab0a4057e253b5","_kg_hide-input":true},"cell_type":"code","source":"temp = train_df[(train_df['totals.transactionRevenue'] >0)]\ndata = temp[['totals.transactionRevenue','date']].groupby('date')['totals.transactionRevenue'].agg(['min','max']).reset_index()\nmean = go.Scatter(x = data['date'], y = data['min'],name = \"Min\",marker = dict(color = '#00E676'))\ncount = go.Scatter(x = data['date'],y = data['max'], name = \"Max\",marker = dict(color = '#00838F'))\npy.iplot([mean,count])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2e06a3cf820fb4f53e70903befd739ecbe5f8fb6"},"cell_type":"markdown","source":"** Summary **\n- I have removed all the non-zero transaction.\n- This graph is to understand the minimum and maximum revenue the company generates.\n- 5th april the company generated the maximum revenue.\n- there are few days where the maximum and minimum revenue are same(Eg - 15 Jan 2017)"},{"metadata":{"trusted":true,"_uuid":"2ebec5781ecd02e274063923136af52e35bcb0d8"},"cell_type":"markdown","source":"# <a id='month'>Revenue based on month</a><br>"},{"metadata":{"trusted":true,"_uuid":"5ac1a3ed4b18cbc7cb06aeef3e39b90778688d4b","_kg_hide-input":true},"cell_type":"code","source":"train_df['month'] = train_df['date'].dt.month\ntrain_df['day'] = train_df['date'].dt.day\ntrain_df['weekday'] = train_df['date'].dt.weekday","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a2613020bf0bd604231bc0b81c294a261ca573be","_kg_hide-input":true},"cell_type":"code","source":"temp = train_df.groupby('month')['totals.transactionRevenue'].agg(['count','mean']).reset_index()\ncount_chart = go.Bar(x = temp['month'], y = temp['count'],name = 'Count',marker = dict(color = \"#E6EE9C\"))\nmean_chart = go.Bar(x = temp['month'],y = temp['mean'], name = 'Mean',marker = dict(color = \"#AFB42B\"))\n\nfig = tools.make_subplots(rows = 1, cols = 2, subplot_titles = ('Total Count', 'Mean Count'))\nfig.append_trace(count_chart,1,1)\nfig.append_trace(mean_chart, 1,2)\npy.iplot(fig)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4cdc7700d97da36047717417387e111bec2f1004"},"cell_type":"markdown","source":"** Summary **\n- It is seen that November month has higest number of visitors but the transaction generated in that month is very low.\n- April month has generated higest mean revenue while its visitors are not high."},{"metadata":{"trusted":true,"_uuid":"00a280a63d799c3383a4558b1377826e85758ee0"},"cell_type":"markdown","source":"# <a id='day'>Revenue based on day</a><br>"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"a585b0400bfdf4634943d867558c689cac4913be"},"cell_type":"code","source":"temp = train_df.groupby('day')['totals.transactionRevenue'].agg(['count','mean']).reset_index()\ncount_chart = go.Bar(x = temp['day'], y = temp['count'],name = 'Count', marker = dict(color = '#1DE9B6'))\nmean_chart = go.Bar(x = temp['day'],y = temp['mean'], name = 'Mean', marker = dict(color = '#00796B'))\n\nfig = tools.make_subplots(rows = 1, cols = 2, subplot_titles = ('Total Count', 'Mean Count'))\nfig.append_trace(count_chart,1,1)\nfig.append_trace(mean_chart, 1,2)\npy.iplot(fig)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"dcefa504ab91b476158e1b563e3672ac43c7c53a"},"cell_type":"markdown","source":"** Summary **\n- It is seen that on 31th day the view count is very less I dont blame any because only Jan, Mar, May, Jul, Aug, Oct, Dec has 31 days.\n- But the intresting fact is that the 2nd highest mean revenue is on day 31st.\n- I think it can either be because of Jan or Dec as they are the start or end of the year."},{"metadata":{"_uuid":"f236ecab34565a2fb3b7b2bd086bd71f54582b0e"},"cell_type":"markdown","source":"# <a id='weekday'>Revenue based on weekday</a><br>"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"ef0f5f57dc0e7b0d9d4d601397afe5d2874e2116"},"cell_type":"code","source":"temp = train_df.groupby('weekday')['totals.transactionRevenue'].agg(['count','mean']).reset_index()\ncount_chart = go.Bar(x = temp['weekday'], y = temp['count'],name = 'Count', marker = dict(color = '#9575CD'))\nmean_chart = go.Bar(x = temp['weekday'],y = temp['mean'], name = 'Mean', marker = dict(color = '#B388FF'))\n\nfig = tools.make_subplots(rows = 1, cols = 2, subplot_titles = ('Total Count', 'Mean Count'))\nfig.append_trace(count_chart,1,1)\nfig.append_trace(mean_chart, 1,2)\npy.iplot(fig)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3119cbd4eae88d97ef982da8af6099c57e9246ba"},"cell_type":"markdown","source":"** Summary **\n- Most of the revenue is generated on monday.\n- very less revenue is generated on weekend."},{"metadata":{"trusted":true,"_uuid":"6a5a7dfba756df8cafde104a34a4ea17906c15cf"},"cell_type":"markdown","source":"# <a id='adcontent'>Most Ad Content</a><br>"},{"metadata":{"trusted":true,"_uuid":"d0d069da4f21ce57c8095fa99315c1581f9fbb2e","_kg_hide-input":true},"cell_type":"code","source":"train_df['trafficSource.adContent'] = train_df['trafficSource.adContent'].fillna('')\nwordcloud2 = WordCloud(width=800, height=400).generate(' '.join(train_df['trafficSource.adContent']))\nplt.figure( figsize=(15,20))\nplt.imshow(wordcloud2)\nplt.axis(\"off\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d2983eba2f97e2ec04ac61f729663a5faaefd950"},"cell_type":"markdown","source":"** Summary **\n- Image speak for it self"},{"metadata":{"trusted":true,"_uuid":"dbbc7f66a41a2f1ae7e0346d32650a81f5009aea"},"cell_type":"markdown","source":"# <a id='keywords'>Keywords used by users</a><br>"},{"metadata":{"trusted":true,"_uuid":"6bc9488753387f1b6a6c2c1e87fc0f06a1283c0d","_kg_hide-input":true},"cell_type":"code","source":"train_df['trafficSource.keyword'] = train_df['trafficSource.keyword'].fillna('')\nwordcloud2 = WordCloud(width=800, height=400).generate(' '.join(train_df['trafficSource.keyword']))\nplt.figure( figsize=(20,20) )\nplt.imshow(wordcloud2)\nplt.axis(\"off\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a4a571a7060578875a0cbdb1e29ca68b895d5769"},"cell_type":"markdown","source":"<a id='source'>Source from where users came</a><br>"},{"metadata":{"trusted":true,"_uuid":"86a627ef868a621057f70ca573039cf88c9ddb06","_kg_hide-input":true},"cell_type":"code","source":"train_df['trafficSource.source'] = train_df['trafficSource.source'].fillna('')\nwordcloud2 = WordCloud(width=800, height=400).generate(' '.join(train_df['trafficSource.source']))\nplt.figure( figsize=(15,20) )\nplt.imshow(wordcloud2)\nplt.axis(\"off\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"af7069f8fb5cef6c13246bd2070d40893b083751"},"cell_type":"markdown","source":"# Glad that you made it till end.\n# Please Upvote to boost me :-)."},{"metadata":{"trusted":true,"_uuid":"f5e5af51b3b8397c53ae45bcc5f88f56b9451f9e"},"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}