{ "cells": [ { "cell_type": "raw", "metadata": {}, "source": [ "---\n", "title: \"Load a CSV File With Python and Pandas\"\n", "author: \"Andrew Bancroft\"\n", "date: 2019-05-22\n", "description: \"Several examples of how to load a csv file into a Pandas dataframe with Python\"\n", "type: technical_note\n", "draft: false\n", "comments: true\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
\n", " Resources\n", "
\n", "
\n", " Right-click -> Save as...\n", "
\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prerequisites" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Install the [Pandas](https://pandas.pydata.org/) library for your Python environment\n", "* Cells in this notebook expect the Car Sales.csv file to be in certain locations; specifics are in the cell itself\n", "* [Resources](#resources) to help you practice" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## First Things First" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load Data From a CSV File" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### File is in the same directory as your Jupyter Notebook" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
DealershipNameRedCarsSilverCarsBlackCarsBlueCarsMonthSoldYearSold
0Clyde's Clunkers902.0650.0754.0792.01.02018.0
1Clyde's Clunkers710.0476.0518.0492.02.02018.0
2Clyde's Clunkers248.0912.0606.0350.03.02018.0
3Clyde's Clunkers782.0912.0858.0446.04.02018.0
4Clyde's Clunkers278.0354.0482.0752.05.02018.0
\n", "
" ], "text/plain": [ " DealershipName RedCars SilverCars BlackCars BlueCars MonthSold \\\n", "0 Clyde's Clunkers 902.0 650.0 754.0 792.0 1.0 \n", "1 Clyde's Clunkers 710.0 476.0 518.0 492.0 2.0 \n", "2 Clyde's Clunkers 248.0 912.0 606.0 350.0 3.0 \n", "3 Clyde's Clunkers 782.0 912.0 858.0 446.0 4.0 \n", "4 Clyde's Clunkers 278.0 354.0 482.0 752.0 5.0 \n", "\n", " YearSold \n", "0 2018.0 \n", "1 2018.0 \n", "2 2018.0 \n", "3 2018.0 \n", "4 2018.0 " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Read the CSV file\n", "car_sales_data = pd.read_csv(\"Car Sales.csv\")\n", "\n", "# Show the first 5 rows\n", "car_sales_data.head(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### File is in a different directory than your Jupyter Notebook\n", "The example will use your \"home directory\" to make this example applicable across operating systems, but you can use any path as long as the file exists there..." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "from os.path import expanduser as ospath\n", "\n", "user_home_directory = ospath(\"~\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
DealershipNameRedCarsSilverCarsBlackCarsBlueCarsMonthSoldYearSold
0Clyde's Clunkers902.0650.0754.0792.01.02018.0
1Clyde's Clunkers710.0476.0518.0492.02.02018.0
2Clyde's Clunkers248.0912.0606.0350.03.02018.0
3Clyde's Clunkers782.0912.0858.0446.04.02018.0
4Clyde's Clunkers278.0354.0482.0752.05.02018.0
\n", "
" ], "text/plain": [ " DealershipName RedCars SilverCars BlackCars BlueCars MonthSold \\\n", "0 Clyde's Clunkers 902.0 650.0 754.0 792.0 1.0 \n", "1 Clyde's Clunkers 710.0 476.0 518.0 492.0 2.0 \n", "2 Clyde's Clunkers 248.0 912.0 606.0 350.0 3.0 \n", "3 Clyde's Clunkers 782.0 912.0 858.0 446.0 4.0 \n", "4 Clyde's Clunkers 278.0 354.0 482.0 752.0 5.0 \n", "\n", " YearSold \n", "0 2018.0 \n", "1 2018.0 \n", "2 2018.0 \n", "3 2018.0 \n", "4 2018.0 " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Make sure to use \"/\" slashes and not \"\\\" slashes\n", "# There actually needs to be folders named \"Path\" and \"To\" and \"CSV\" and \"File\"\n", "# in your home directory (the \"~\" means \"home directory\") for this cell to work\n", "csv_file_path = user_home_directory + \"/Path/To/CSV/File/Car Sales.csv\"\n", "\n", "other_path_car_sales_data = pd.read_csv(csv_file_path)\n", "\n", "# Show the first 5 rows\n", "other_path_car_sales_data.head(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### From a URL" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
DealershipNameRedCarsSilverCarsBlackCarsBlueCarsMonthSoldYearSold
0Clyde's Clunkers902.0650.0754.0792.01.02018.0
1Clyde's Clunkers710.0476.0518.0492.02.02018.0
2Clyde's Clunkers248.0912.0606.0350.03.02018.0
3Clyde's Clunkers782.0912.0858.0446.04.02018.0
4Clyde's Clunkers278.0354.0482.0752.05.02018.0
\n", "
" ], "text/plain": [ " DealershipName RedCars SilverCars BlackCars BlueCars MonthSold \\\n", "0 Clyde's Clunkers 902.0 650.0 754.0 792.0 1.0 \n", "1 Clyde's Clunkers 710.0 476.0 518.0 492.0 2.0 \n", "2 Clyde's Clunkers 248.0 912.0 606.0 350.0 3.0 \n", "3 Clyde's Clunkers 782.0 912.0 858.0 446.0 4.0 \n", "4 Clyde's Clunkers 278.0 354.0 482.0 752.0 5.0 \n", "\n", " YearSold \n", "0 2018.0 \n", "1 2018.0 \n", "2 2018.0 \n", "3 2018.0 \n", "4 2018.0 " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Note the URL Encoding with \"%20\" for spaces\n", "url_to_csv_file = \"https://github.com/andrewcbancroft/datadaylife-blog/raw/master/datasets/Car%20Sales.csv\"\n", "\n", "# Read the CSV file\n", "url_car_sales_data = pd.read_csv(url_to_csv_file)\n", "\n", "# Show the first 5 rows\n", "url_car_sales_data.head(5)" ] } ], "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.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }