{ "cells": [ { "cell_type": "code", "execution_count": 82, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pandas as pd\n", "star_wars = pd.read_csv(\"star_wars.csv\", encoding=\"ISO-8859-1\")" ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "collapsed": true }, "outputs": [], "source": [ "star_wars = star_wars[pd.notnull(star_wars[\"RespondentID\"])]" ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "collapsed": false }, "outputs": [], "source": [ "star_wars.head()" ] }, { "cell_type": "code", "execution_count": 85, "metadata": { "collapsed": false }, "outputs": [], "source": [ "star_wars.columns" ] }, { "cell_type": "code", "execution_count": 86, "metadata": { "collapsed": false }, "outputs": [], "source": [ "yes_no = {\"Yes\": True, \"No\": False}\n", "\n", "for col in [\n", " \"Have you seen any of the 6 films in the Star Wars franchise?\",\n", " \"Do you consider yourself to be a fan of the Star Wars film franchise?\"\n", " ]:\n", " star_wars[col] = star_wars[col].map(yes_no)\n", "\n", "star_wars.head()" ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "\n", "movie_mapping = {\n", " \"Star Wars: Episode I The Phantom Menace\": True,\n", " np.nan: False,\n", " \"Star Wars: Episode II Attack of the Clones\": True,\n", " \"Star Wars: Episode III Revenge of the Sith\": True,\n", " \"Star Wars: Episode IV A New Hope\": True,\n", " \"Star Wars: Episode V The Empire Strikes Back\": True,\n", " \"Star Wars: Episode VI Return of the Jedi\": True\n", "}\n", "\n", "for col in star_wars.columns[3:9]:\n", " star_wars[col] = star_wars[col].map(movie_mapping)" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "collapsed": false }, "outputs": [], "source": [ "star_wars = star_wars.rename(columns={\n", " \"Which of the following Star Wars films have you seen? Please select all that apply.\": \"seen_1\",\n", " \"Unnamed: 4\": \"seen_2\",\n", " \"Unnamed: 5\": \"seen_3\",\n", " \"Unnamed: 6\": \"seen_4\",\n", " \"Unnamed: 7\": \"seen_5\",\n", " \"Unnamed: 8\": \"seen_6\"\n", " })\n", "\n", "star_wars.head()" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "collapsed": false }, "outputs": [], "source": [ "star_wars = star_wars.rename(columns={\n", " \"Please rank the Star Wars films in order of preference with 1 being your favorite film in the franchise and 6 being your least favorite film.\": \"ranking_1\",\n", " \"Unnamed: 10\": \"ranking_2\",\n", " \"Unnamed: 11\": \"ranking_3\",\n", " \"Unnamed: 12\": \"ranking_4\",\n", " \"Unnamed: 13\": \"ranking_5\",\n", " \"Unnamed: 14\": \"ranking_6\"\n", " })\n", "\n", "star_wars.head()" ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "collapsed": false }, "outputs": [], "source": [ "star_wars[star_wars.columns[9:15]] = star_wars[star_wars.columns[9:15]].astype(float)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "collapsed": false }, "outputs": [], "source": [ "star_wars[star_wars.columns[9:15]].mean()" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "\n", "plt.bar(range(6), star_wars[star_wars.columns[9:15]].mean())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Rankings\n", "\n", "So far, we've cleaned up the data, renamed several columns, and computed the average ranking of each movie. As I suspected, it looks like the \"original\" movies are rated much more highly than the newer ones." ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "collapsed": false }, "outputs": [], "source": [ "star_wars[star_wars.columns[3:9]].sum()" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.bar(range(6), star_wars[star_wars.columns[3:9]].sum())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# View counts\n", "\n", "It appears that the original movies were seen by more respondents than the newer movies. This reinforces what we saw in the rankings, where the earlier movies seem to be more popular." ] }, { "cell_type": "code", "execution_count": 98, "metadata": { "collapsed": true }, "outputs": [], "source": [ "males = star_wars[star_wars[\"Gender\"] == \"Male\"]\n", "females = star_wars[star_wars[\"Gender\"] == \"Female\"]" ] }, { "cell_type": "code", "execution_count": 99, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.bar(range(6), males[males.columns[9:15]].mean())\n", "plt.show()\n", "\n", "plt.bar(range(6), females[females.columns[9:15]].mean())\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": 100, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.bar(range(6), males[males.columns[3:9]].sum())\n", "plt.show()\n", "\n", "plt.bar(range(6), females[females.columns[3:9]].sum())\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Male/Female differences in favorite Star Wars movie and most seen movie\n", "\n", "Interestingly, more males watches episodes 1-3, but males liked them far less than females did." ] } ], "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.5.0" } }, "nbformat": 4, "nbformat_minor": 0 }