|
@@ -4,7 +4,7 @@
|
|
"cell_type": "markdown",
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"metadata": {},
|
|
"source": [
|
|
"source": [
|
|
- "# Python Intermediate: Creating a Baby Pandas Class Solutions"
|
|
|
|
|
|
+ "# Python Intermediate: Creating a SimpleFrame Class"
|
|
]
|
|
]
|
|
},
|
|
},
|
|
{
|
|
{
|
|
@@ -34,13 +34,11 @@
|
|
"To preview our data, we’ll need to:\n",
|
|
"To preview our data, we’ll need to:\n",
|
|
"- Be able to view the first five rows\n",
|
|
"- Be able to view the first five rows\n",
|
|
"- Be able to view the shape of our data\n",
|
|
"- Be able to view the shape of our data\n",
|
|
- "- Be able to view the data types for each column\n",
|
|
|
|
"\n",
|
|
"\n",
|
|
"To manipulate our data, we’ll need to: \n",
|
|
"To manipulate our data, we’ll need to: \n",
|
|
"- Add new columns\n",
|
|
"- Add new columns\n",
|
|
"- Be able to apply values to columns\n",
|
|
"- Be able to apply values to columns\n",
|
|
"- Be able to subset our data\n",
|
|
"- Be able to subset our data\n",
|
|
- "- Change the data type\n",
|
|
|
|
"\n",
|
|
"\n",
|
|
"To make calculations, we’ll need to:\n",
|
|
"To make calculations, we’ll need to:\n",
|
|
"- Finding the minimum\n",
|
|
"- Finding the minimum\n",
|
|
@@ -55,57 +53,45 @@
|
|
"source": [
|
|
"source": [
|
|
"# Translating our words into objects\n",
|
|
"# Translating our words into objects\n",
|
|
"\n",
|
|
"\n",
|
|
- "BabyPandas -> Class\n",
|
|
|
|
- "\n",
|
|
|
|
- "Load -> Method\n",
|
|
|
|
- "\n",
|
|
|
|
- "Data -> Attribute\n",
|
|
|
|
- "\n",
|
|
|
|
- "Columns -> Attribute\n",
|
|
|
|
|
|
+ "- SimpleFrame -> Class\n",
|
|
|
|
+ "- Load -> Method\n",
|
|
|
|
+ "- Data -> Attribute\n",
|
|
|
|
+ "- Columns -> Attribute\n",
|
|
"\n",
|
|
"\n",
|
|
"## Preview\n",
|
|
"## Preview\n",
|
|
"\n",
|
|
"\n",
|
|
- "View the first five rows -> Method\n",
|
|
|
|
- "\n",
|
|
|
|
- "View num of rows/cols of our data -> Method\n",
|
|
|
|
- "\n",
|
|
|
|
- "View the data types for each column -> Method\n",
|
|
|
|
- "\n",
|
|
|
|
|
|
+ "- View the first five rows -> Method\n",
|
|
|
|
+ "- View num of rows/cols of our data -> Method\n",
|
|
"\n",
|
|
"\n",
|
|
"## Manipulate\n",
|
|
"## Manipulate\n",
|
|
"\n",
|
|
"\n",
|
|
- "Add new columns -> Method\n",
|
|
|
|
- "\n",
|
|
|
|
- "Apply values to columns -> Method\n",
|
|
|
|
- "\n",
|
|
|
|
- "subset our data -> Method\n",
|
|
|
|
- "\n",
|
|
|
|
- "Change the data type -> Method\n",
|
|
|
|
- "\n",
|
|
|
|
|
|
+ "- Add new columns -> Method\n",
|
|
|
|
+ "- Apply values to columns -> Method\n",
|
|
|
|
+ "- Subset our data -> Method\n",
|
|
"\n",
|
|
"\n",
|
|
"## Calculations\n",
|
|
"## Calculations\n",
|
|
"\n",
|
|
"\n",
|
|
- "Minimum -> Method\n",
|
|
|
|
- "\n",
|
|
|
|
- "Maximum -> Method\n",
|
|
|
|
- "\n",
|
|
|
|
- "Mean -> Method\n",
|
|
|
|
- "\n",
|
|
|
|
- "Standard deviation -> Method"
|
|
|
|
|
|
+ "- Minimum -> Method\n",
|
|
|
|
+ "- Maximum -> Method\n",
|
|
|
|
+ "- Mean -> Method\n",
|
|
|
|
+ "- Standard deviation -> Method"
|
|
]
|
|
]
|
|
},
|
|
},
|
|
{
|
|
{
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
- "execution_count": 31,
|
|
|
|
|
|
+ "execution_count": 1,
|
|
"metadata": {},
|
|
"metadata": {},
|
|
"outputs": [
|
|
"outputs": [
|
|
{
|
|
{
|
|
- "name": "stdout",
|
|
|
|
- "output_type": "stream",
|
|
|
|
- "text": [
|
|
|
|
- "2\n",
|
|
|
|
- "['Despacito (Featuring Daddy Yankee)', 'Luis Fonsi', 64238]\n",
|
|
|
|
- "['Por Fin Te Encontré', 'Cali Y El Dandee', 1993]\n"
|
|
|
|
|
|
+ "ename": "FileNotFoundError",
|
|
|
|
+ "evalue": "[Errno 2] No such file or directory: 'music_data.csv'",
|
|
|
|
+ "output_type": "error",
|
|
|
|
+ "traceback": [
|
|
|
|
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
|
|
|
+ "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
|
|
|
|
+ "\u001b[0;32m<ipython-input-1-49ed6515bc96>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 95\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 96\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mBabyPandas\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"music_data.csv\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 97\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 98\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 99\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
|
|
+ "\u001b[0;32m<ipython-input-1-49ed6515bc96>\u001b[0m in \u001b[0;36mread_data\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0mReads\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mopens\u001b[0m \u001b[0mthe\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m '''\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0mf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfilename\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"r\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcsv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreader\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolumns\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
|
|
+ "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'music_data.csv'"
|
|
]
|
|
]
|
|
}
|
|
}
|
|
],
|
|
],
|
|
@@ -130,12 +116,6 @@
|
|
" Displays the first five rows\n",
|
|
" Displays the first five rows\n",
|
|
" '''\n",
|
|
" '''\n",
|
|
" return self.data[:5]\n",
|
|
" return self.data[:5]\n",
|
|
- " \n",
|
|
|
|
- " def info(self):\n",
|
|
|
|
- " types = []\n",
|
|
|
|
- " for value in self.data[1]:\n",
|
|
|
|
- " types.append(type(value))\n",
|
|
|
|
- " return types\n",
|
|
|
|
" \n",
|
|
" \n",
|
|
" \n",
|
|
" \n",
|
|
" def shape(self):\n",
|
|
" def shape(self):\n",
|
|
@@ -161,14 +141,6 @@
|
|
" for data in self.data[1:]:\n",
|
|
" for data in self.data[1:]:\n",
|
|
" data[column_index] = new_value\n",
|
|
" data[column_index] = new_value\n",
|
|
" \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",
|
|
" def subset(self, column_name, row_value):\n",
|
|
" for pos, col in enumerate(self.data[0]):\n",
|
|
" for pos, col in enumerate(self.data[0]):\n",
|
|
" if col == column_name:\n",
|
|
" if col == column_name:\n",
|
|
@@ -222,11 +194,9 @@
|
|
"s = BabyPandas(\"music_data.csv\")\n",
|
|
"s = BabyPandas(\"music_data.csv\")\n",
|
|
"s.read_data()\n",
|
|
"s.read_data()\n",
|
|
"\n",
|
|
"\n",
|
|
- "s.info()\n",
|
|
|
|
"s.shape()\n",
|
|
"s.shape()\n",
|
|
"s.columns\n",
|
|
"s.columns\n",
|
|
"s.new_column('hello')\n",
|
|
"s.new_column('hello')\n",
|
|
- "s.change_type('Streams',int)\n",
|
|
|
|
"s.subset(\"Artist\",\"Shakira\")\n",
|
|
"s.subset(\"Artist\",\"Shakira\")\n",
|
|
"print(s.maximum(\"Streams\"))\n",
|
|
"print(s.maximum(\"Streams\"))\n",
|
|
"print(s.minimum(\"Streams\"))"
|
|
"print(s.minimum(\"Streams\"))"
|