{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This IPython notebook illustrates how to read the CSV files from disk as tables and set their metadata." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we need to import *py_entitymatching* package and other libraries as follows:\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/pradap/miniconda3/lib/python3.5/site-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.\n", " \"This module will be removed in 0.20.\", DeprecationWarning)\n" ] } ], "source": [ "import py_entitymatching as em\n", "import pandas as pd\n", "import os, sys" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get the Path of the CSV File" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " the paths of the CSV file in the disk. For the convenience of the user, we have included some sample files in the package. The path of a sample CSV file can be obtained like this:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Get the datasets directory\n", "datasets_dir = em.get_install_path() + os.sep + 'datasets'\n", "\n", "# Get the paths of the input tables\n", "path_A = datasets_dir + os.sep + 'person_table_A.csv'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ID,name,birth_year,hourly_wage,address,zipcode\r\n", "a1,Kevin Smith,1989,30,\"607 From St, San Francisco\",94107\r\n", "a2,Michael Franklin,1988,27.5,\"1652 Stockton St, San Francisco\",94122\r\n" ] } ], "source": [ "# Display the contents of the file in path_A\n", "!cat $path_A | head -3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ways to Read a CSV File and Set Metadata" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are three different ways to read a CSV file and set metadata:\n", "\n", "1. Read a CSV file first, and then set the metadata\n", "2. Read a CSV file and set the metadata together\n", "3. Read a CSV file and set the metadata from a file in disk " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Read the CSV file First and Then Set the Metadata" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, read the CSV files as follows:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "A = em.read_csv_metadata(path_A)\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": 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", "
IDnamebirth_yearhourly_wageaddresszipcode
0a1Kevin Smith198930.0607 From St, San Francisco94107
1a2Michael Franklin198827.51652 Stockton St, San Francisco94122
2a3William Bridge198632.03131 Webster St, San Francisco94107
3a4Binto George198732.5423 Powell St, San Francisco94122
4a5Alphonse Kemper198435.01702 Post Street, San Francisco94122
\n", "
" ], "text/plain": [ " ID name birth_year hourly_wage \\\n", "0 a1 Kevin Smith 1989 30.0 \n", "1 a2 Michael Franklin 1988 27.5 \n", "2 a3 William Bridge 1986 32.0 \n", "3 a4 Binto George 1987 32.5 \n", "4 a5 Alphonse Kemper 1984 35.0 \n", "\n", " address zipcode \n", "0 607 From St, San Francisco 94107 \n", "1 1652 Stockton St, San Francisco 94122 \n", "2 3131 Webster St, San Francisco 94107 \n", "3 423 Powell St, San Francisco 94122 \n", "4 1702 Post Street, San Francisco 94122 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.head()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "pandas.core.frame.DataFrame" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Display the 'type' of A \n", "type(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then set the metadata for the table. We see `ID` is the key attribute (since it contains unique values and no value is missing) for the table. We can set this metadata as follows:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "em.set_key(A, 'ID')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'ID'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get the metadata that were set for table A\n", "em.get_key(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now the CSV file is read into the memory and the metadata (i.e. key) is set for the table. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Read a CSV File and Set Metadata Together" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the above, we saw that we first read in the CSV file and then set the metadata. These two steps can be combined into a single step like this:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "A = em.read_csv_metadata(path_A, key='ID')\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "pandas.core.frame.DataFrame" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Display the 'type' of A\n", "type(A)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'ID'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get the metadata that were set for the table A \n", "em.get_key(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Read a CSV File and Set Metadata from a File in Disk" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The user can specify the metadata in a file.\n", "\n", "This file *MUST* be in the same directory as the CSV file and the file name \n", "should be same, except the extension is set to '.metadata'." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# We set the metadata for table A (stored in person_table_A.csv).\n", "\n", "# Get the file name (with full path) where the metadata file must be stored\n", "metadata_file = datasets_dir + os.sep + 'person_table_A.metadata'\n", "\n", "# Specify the metadata for table A . Here we specify that 'ID' is the key attribute for the table. \n", "# Note that this step requires write permission to the datasets directory.\n", "!echo '#key=ID' > $metadata_file\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# If you donot have write permissions to the datasets directory, first copy the file to the local directory and then create\n", "# a metadata file like this:\n", "\n", "# !cp $path_A .\n", "# metadata_local_file = 'person_table_A.metadata'\n", "# !echo '#key=ID' > $metadata_local_file" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Read the CSV file for table A\n", "A = em.read_csv_metadata(path_A)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'ID'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get the key for table A\n", "em.get_key(A)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Remove the metadata file\n", "!rm $metadata_file" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [Root]", "language": "python", "name": "Python [Root]" }, "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.2" } }, "nbformat": 4, "nbformat_minor": 0 }