|
@@ -0,0 +1,147 @@
|
|
|
+{
|
|
|
+ "cells": [
|
|
|
+ {
|
|
|
+ "cell_type": "markdown",
|
|
|
+ "metadata": {},
|
|
|
+ "source": [
|
|
|
+ "# Python Intermediate: Creating a Baby Pandas Class Solutions"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": null,
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [],
|
|
|
+ "source": [
|
|
|
+ "import csv\n",
|
|
|
+ "from statistics import mean, stdev, median, mode\n",
|
|
|
+ "\n",
|
|
|
+ "class BabyPandas():\n",
|
|
|
+ " def __init__(self, filename):\n",
|
|
|
+ " self.filename = filename\n",
|
|
|
+ " \n",
|
|
|
+ " def read_data(self):\n",
|
|
|
+ " '''\n",
|
|
|
+ " Reads and opens the data\n",
|
|
|
+ " '''\n",
|
|
|
+ " f = open(self.filename,\"r\")\n",
|
|
|
+ " self.data = list(csv.reader(f))\n",
|
|
|
+ " self.columns = self.data[0]\n",
|
|
|
+ " \n",
|
|
|
+ " def head(self):\n",
|
|
|
+ " '''\n",
|
|
|
+ " Displays the first five rows\n",
|
|
|
+ " '''\n",
|
|
|
+ " return self.data[:5]\n",
|
|
|
+ " \n",
|
|
|
+ " def new_column(self, column_name):\n",
|
|
|
+ " for pos, d in enumerate(self.data):\n",
|
|
|
+ " if pos == 0:\n",
|
|
|
+ " d.append(column_name)\n",
|
|
|
+ " else:\n",
|
|
|
+ " d.append('NA')\n",
|
|
|
+ " \n",
|
|
|
+ " def apply(self, column_name, new_value):\n",
|
|
|
+ " for pos, col in enumerate(self.data[0]):\n",
|
|
|
+ " if col == column_name:\n",
|
|
|
+ " column_index = pos\n",
|
|
|
+ " \n",
|
|
|
+ " for data in self.data[1:]:\n",
|
|
|
+ " data[column_index] = new_value\n",
|
|
|
+ " \n",
|
|
|
+ " def change_type(self, column_name, function):\n",
|
|
|
+ " for pos, col in enumerate(self.data[0]):\n",
|
|
|
+ " if col == column_name:\n",
|
|
|
+ " column_index = pos\n",
|
|
|
+ " \n",
|
|
|
+ " for data in self.data[1:]:\n",
|
|
|
+ " data[column_index] = function(data[column_index])\n",
|
|
|
+ " \n",
|
|
|
+ " def subset(self, column_name, row_value):\n",
|
|
|
+ " for pos, col in enumerate(self.data[0]):\n",
|
|
|
+ " if col == column_name:\n",
|
|
|
+ " column_index = pos\n",
|
|
|
+ " \n",
|
|
|
+ " print(column_index)\n",
|
|
|
+ " subset_data = []\n",
|
|
|
+ " for data in self.data[1:]:\n",
|
|
|
+ " if row_value in data:\n",
|
|
|
+ " subset_data.append(data[column_index])\n",
|
|
|
+ " \n",
|
|
|
+ " return subset_data\n",
|
|
|
+ "\n",
|
|
|
+ " \n",
|
|
|
+ " def summary_stats(self, column_name):\n",
|
|
|
+ " for pos, col in enumerate(self.data[0]):\n",
|
|
|
+ " if col == column_name:\n",
|
|
|
+ " column_index = pos\n",
|
|
|
+ "\n",
|
|
|
+ " num_data = [data[column_index] for data in self.data[1:]]\n",
|
|
|
+ " m = statistics.mean(num_data)\n",
|
|
|
+ " std = stdev(num_data)\n",
|
|
|
+ " median = statistics.median(num_data)\n",
|
|
|
+ " \n",
|
|
|
+ " print(\"Mean is {mean}\".format(mean= m))\n",
|
|
|
+ " print(\"Standard Deviation is {std}\".format(std= std))\n",
|
|
|
+ " print(\"Median is {median}\".format(median= median))\n",
|
|
|
+ " \n",
|
|
|
+ " \n",
|
|
|
+ " def minimum(self, column):\n",
|
|
|
+ " for pos, col in enumerate(self.data[0]):\n",
|
|
|
+ " if col == column:\n",
|
|
|
+ " column_index = pos\n",
|
|
|
+ " \n",
|
|
|
+ " print(column_index)\n",
|
|
|
+ " ## Find min value\n",
|
|
|
+ " col_data = []\n",
|
|
|
+ " for row in self.data[1:]:\n",
|
|
|
+ " col_data.append(row[column_index])\n",
|
|
|
+ " \n",
|
|
|
+ " return min(col_data)\n",
|
|
|
+ " \n",
|
|
|
+ " def maximum(self, column):\n",
|
|
|
+ " for pos, col in enumerate(self.data[0]):\n",
|
|
|
+ " if col == column:\n",
|
|
|
+ " column_index = pos\n",
|
|
|
+ " \n",
|
|
|
+ " print(column_index)\n",
|
|
|
+ " ## Find min value\n",
|
|
|
+ " col_data = []\n",
|
|
|
+ " for row in self.data[1:]:\n",
|
|
|
+ " col_data.append(row[column_index])\n",
|
|
|
+ " \n",
|
|
|
+ " return max(col_data)\n",
|
|
|
+ " \n",
|
|
|
+ "s = BabyPandas(\"music_data.csv\")\n",
|
|
|
+ "s.read_data()\n",
|
|
|
+ "s.columns\n",
|
|
|
+ "s.new_column('hello')\n",
|
|
|
+ "s.change_type('Streams',int)\n",
|
|
|
+ "\n",
|
|
|
+ "\n",
|
|
|
+ "s.subset(\"Artist\",\"Shakira\")"
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ "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.0"
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "nbformat": 4,
|
|
|
+ "nbformat_minor": 2
|
|
|
+}
|