Created
June 3, 2018 00:53
-
-
Save jrjames83/95923106b1ffe6eedaeff141f7eab8ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import pandas as pd" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<style>\n", | |
| " .dataframe thead tr:only-child th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: left;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>Age</th>\n", | |
| " <th>Gender</th>\n", | |
| " <th>Item ID</th>\n", | |
| " <th>Item Name</th>\n", | |
| " <th>Price</th>\n", | |
| " <th>SN</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>38</td>\n", | |
| " <td>Male</td>\n", | |
| " <td>165</td>\n", | |
| " <td>Bone Crushing Silver Skewer</td>\n", | |
| " <td>3.37</td>\n", | |
| " <td>Aelalis34</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>21</td>\n", | |
| " <td>Male</td>\n", | |
| " <td>119</td>\n", | |
| " <td>Stormbringer, Dark Blade of Ending Misery</td>\n", | |
| " <td>2.32</td>\n", | |
| " <td>Eolo46</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>34</td>\n", | |
| " <td>Male</td>\n", | |
| " <td>174</td>\n", | |
| " <td>Primitive Blade</td>\n", | |
| " <td>2.46</td>\n", | |
| " <td>Assastnya25</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>21</td>\n", | |
| " <td>Male</td>\n", | |
| " <td>92</td>\n", | |
| " <td>Final Critic</td>\n", | |
| " <td>1.36</td>\n", | |
| " <td>Pheusrical25</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4</th>\n", | |
| " <td>23</td>\n", | |
| " <td>Male</td>\n", | |
| " <td>63</td>\n", | |
| " <td>Stormfury Mace</td>\n", | |
| " <td>1.27</td>\n", | |
| " <td>Aela59</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " Age Gender Item ID Item Name Price \\\n", | |
| "0 38 Male 165 Bone Crushing Silver Skewer 3.37 \n", | |
| "1 21 Male 119 Stormbringer, Dark Blade of Ending Misery 2.32 \n", | |
| "2 34 Male 174 Primitive Blade 2.46 \n", | |
| "3 21 Male 92 Final Critic 1.36 \n", | |
| "4 23 Male 63 Stormfury Mace 1.27 \n", | |
| "\n", | |
| " SN \n", | |
| "0 Aelalis34 \n", | |
| "1 Eolo46 \n", | |
| "2 Assastnya25 \n", | |
| "3 Pheusrical25 \n", | |
| "4 Aela59 " | |
| ] | |
| }, | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "df = pd.read_json('purchase_data.json')\n", | |
| "df.head()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<style>\n", | |
| " .dataframe thead tr:only-child th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: left;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>Gender</th>\n", | |
| " <th>Age</th>\n", | |
| " <th>Item ID</th>\n", | |
| " <th>Price</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>Female</td>\n", | |
| " <td>22.558824</td>\n", | |
| " <td>88.110294</td>\n", | |
| " <td>2.815515</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>Male</td>\n", | |
| " <td>22.685624</td>\n", | |
| " <td>91.571880</td>\n", | |
| " <td>2.950521</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>Other / Non-Disclosed</td>\n", | |
| " <td>27.363636</td>\n", | |
| " <td>114.636364</td>\n", | |
| " <td>3.249091</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " Gender Age Item ID Price\n", | |
| "0 Female 22.558824 88.110294 2.815515\n", | |
| "1 Male 22.685624 91.571880 2.950521\n", | |
| "2 Other / Non-Disclosed 27.363636 114.636364 3.249091" | |
| ] | |
| }, | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# Create a dataframe by grouping, taking an aggregation and resetting the index\n", | |
| "grouped = df.groupby('Gender').mean().reset_index()\n", | |
| "grouped" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'$ 2.815514705882352'" | |
| ] | |
| }, | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "\"$ {}\".format(float(df.groupby('Gender').mean().reset_index().iloc[0]['Price'])) # Row 0 is now Female" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'$ 2.9505213270142154'" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "\"$ {}\".format(float(df.groupby('Gender').mean().reset_index().iloc[1]['Price'])) # Row 0 is now Female" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<style>\n", | |
| " .dataframe thead tr:only-child th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: left;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>Gender</th>\n", | |
| " <th>Age</th>\n", | |
| " <th>Item ID</th>\n", | |
| " <th>Price</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>Male</td>\n", | |
| " <td>22.685624</td>\n", | |
| " <td>91.57188</td>\n", | |
| " <td>2.950521</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " Gender Age Item ID Price\n", | |
| "1 Male 22.685624 91.57188 2.950521" | |
| ] | |
| }, | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "grouped = df.groupby('Gender').mean().reset_index()\n", | |
| "grouped.query('Gender == \"Male\" ')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "2.9505213270142154" | |
| ] | |
| }, | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "grouped = df.groupby('Gender').mean().reset_index()\n", | |
| "grouped.query('Gender == \"Male\" ').Price.values[0] # Another way to get mean male price" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'Age': {0: 22.558823529411764, 1: 22.685624012638229, 2: 27.363636363636363},\n", | |
| " 'Gender': {0: 'Female', 1: 'Male', 2: 'Other / Non-Disclosed'},\n", | |
| " 'Item ID': {0: 88.110294117647058,\n", | |
| " 1: 91.571879936808841,\n", | |
| " 2: 114.63636363636364},\n", | |
| " 'Price': {0: 2.815514705882352, 1: 2.9505213270142154, 2: 3.2490909090909086}}" | |
| ] | |
| }, | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "grouped.to_dict() # Or convert the grouped object to a large dictionary" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "dict_keys(['Gender', 'Age', 'Item ID', 'Price'])" | |
| ] | |
| }, | |
| "execution_count": 9, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "gd = grouped.to_dict()\n", | |
| "gd.keys()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "dict_values(['Female', 'Male', 'Other / Non-Disclosed'])\n", | |
| "dict_values([2.815514705882352, 2.9505213270142154, 3.2490909090909086])\n" | |
| ] | |
| }, | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'Female': 2.815514705882352,\n", | |
| " 'Male': 2.9505213270142154,\n", | |
| " 'Other / Non-Disclosed': 3.2490909090909086}" | |
| ] | |
| }, | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "print(gd['Gender'].values())\n", | |
| "print(gd['Price'].values())\n", | |
| "# Zip the Gender values and prices together to get a lookup\n", | |
| "dict(zip(gd['Gender'].values(), gd['Price'].values()))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "2.815514705882352" | |
| ] | |
| }, | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "result = dict(zip(gd['Gender'].values(), gd['Price'].values()))\n", | |
| "result['Female']" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.6.1" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment