This IPython notebook illustrates how to read the CSV files from disk as tables and set their metadata.
First, we need to import py_entitymatching package and other libraries as follows:
import py_entitymatching as em
import pandas as pd
import os, sys
/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. "This module will be removed in 0.20.", DeprecationWarning)
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:
# Get the datasets directory
datasets_dir = em.get_install_path() + os.sep + 'datasets'
# Get the paths of the input tables
path_A = datasets_dir + os.sep + 'person_table_A.csv'
# Display the contents of the file in path_A
!cat $path_A | head -3
ID,name,birth_year,hourly_wage,address,zipcode a1,Kevin Smith,1989,30,"607 From St, San Francisco",94107 a2,Michael Franklin,1988,27.5,"1652 Stockton St, San Francisco",94122
There are three different ways to read a CSV file and set metadata:
First, read the CSV files as follows:
A = em.read_csv_metadata(path_A)
A.head()
ID | name | birth_year | hourly_wage | address | zipcode | |
---|---|---|---|---|---|---|
0 | a1 | Kevin Smith | 1989 | 30.0 | 607 From St, San Francisco | 94107 |
1 | a2 | Michael Franklin | 1988 | 27.5 | 1652 Stockton St, San Francisco | 94122 |
2 | a3 | William Bridge | 1986 | 32.0 | 3131 Webster St, San Francisco | 94107 |
3 | a4 | Binto George | 1987 | 32.5 | 423 Powell St, San Francisco | 94122 |
4 | a5 | Alphonse Kemper | 1984 | 35.0 | 1702 Post Street, San Francisco | 94122 |
# Display the 'type' of A
type(A)
pandas.core.frame.DataFrame
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:
em.set_key(A, 'ID')
True
# Get the metadata that were set for table A
em.get_key(A)
'ID'
Now the CSV file is read into the memory and the metadata (i.e. key) is set for the table.
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:
A = em.read_csv_metadata(path_A, key='ID')
# Display the 'type' of A
type(A)
pandas.core.frame.DataFrame
# Get the metadata that were set for the table A
em.get_key(A)
'ID'
The user can specify the metadata in a file.
This file MUST be in the same directory as the CSV file and the file name should be same, except the extension is set to '.metadata'.
# We set the metadata for table A (stored in person_table_A.CSV).
# Get the path where the metadata file must be stored
metadata_file = datasets_dir + os.sep + 'person_table_A.metadata'
# Specify the metadata for table A . Here we specify that 'ID' is the key attribute for the table. Note that this step
# requires write permission to the datasets directory.
!echo '#key=ID' > $metadata_file
# Read the CSV file for table A
A = em.read_csv_metadata(path_A)
em.get_key(A)
'ID'
!rm $metadata_file