Lyonk71 / pandas-dedupe

Simplifies use of the Dedupe library via Pandas
134 stars 30 forks source link

pandas-dedupe

The Dedupe library made easy with Pandas.

Installation

pip install pandas-dedupe

Video Tutorials

Basic Deduplication

Basic Usage

A training file and a settings file will be created while running Dedupe. Keeping these files will eliminate the need to retrain your model in the future.

If you would like to retrain your model from scratch, just delete the settings and training files.

Deduplication (dedupe_dataframe)

dedupe_dataframe is for deduplication when you have data that can contain multiple records that can all refer to the same entity

import pandas as pd
import pandas_dedupe

#load dataframe
df = pd.read_csv('test_names.csv')

#initiate deduplication
df_final = pandas_dedupe.dedupe_dataframe(df,['first_name', 'last_name', 'middle_initial'])

#send output to csv
df_final.to_csv('deduplication_output.csv')

Gazetteer deduplication (gazetteer_dataframe)

gazetteer_dataframe is for matching a messy dataset against a 'canonical dataset' (i.e. the gazette)

import pandas as pd
import pandas_dedupe

#load dataframe
df_clean = pd.read_csv('gazette.csv')
df_messy = pd.read_csv('test_names.csv')

#initiate deduplication
df_final = pandas_dedupe.gazetteer_dataframe(df_clean, df_messy, 'fullname', canonicalize=True)

#send output to csv
df_final.to_csv('gazetteer_deduplication_output.csv')

Matching / Record Linkage

Use identical field names when linking dataframes. Record linkage should only be used on dataframes that have been deduplicated.

import pandas as pd
import pandas_dedupe

#load dataframes
dfa = pd.read_csv('file_a.csv')
dfb = pd.read_csv('file_b.csv')

#initiate matching
df_final = pandas_dedupe.link_dataframes(dfa, dfb, ['field_1', 'field_2', 'field_3', 'field_4'])

#send output to csv
df_final.to_csv('linkage_output.csv')

Advanced Usage

Canonicalize Fields

The canonicalize parameter will standardize names in a given cluster. Original fields are also kept.

pandas_dedupe.dedupe_dataframe(df,['first_name', 'last_name', 'payment_type'], canonicalize=True)

Update Threshold (dedupe_dataframe and gazetteer_dataframe only)

Group records into clusters only if the cophenetic similarity of the cluster is greater than the threshold.

pandas_dedupe.dedupe_dataframe(df, ['first_name', 'last_name'], threshold=.7)

Update Existing Model (dedupe_dataframe and gazetteer_dataframe only)

If True, it allows a user to update the existing model.

pandas_dedupe.dedupe_dataframe(df, ['first_name', 'last_name'], update_model=True)

Update Sample Size

Specifies the sample size used for training as a float from 0 to 1. By default it is 30% (0.3) of our data.

pandas_dedupe.dedupe_dataframe(df, ['first_name', 'last_name'], sample_size=0.5)

Specifying Types

If you'd like to specify dates, spatial data, etc, do so here. The structure must be like so: ('field', 'type', 'additional_parameter). the additional_parameter section can be omitted. The default type is String.

See the full list of types below.

# Price Example
pandas_dedupe.dedupe_dataframe(df,['first_name', 'last_name', ('salary', 'Price')])

# has missing Example
pandas_dedupe.link_dataframes(df,['SSN', ('bio_pgraph', 'Text'), ('salary', 'Price', 'has missing')])

# crf Example
pandas_dedupe.dedupe_dataframe(df,[('first_name', 'String', 'crf'), 'last_name', (m_initial, 'Exact')])

Types

Dedupe supports a variety of datatypes; a full list with documentation can be found here.

pandas-dedupe officially supports the following datatypes:

Additional supported parameters are:

Contributors

Tyler Marrs - Refactored code, added docstrings, added threshold parameter

Tawni Marrs - refactored code, added docstrings

ieriii - Added update_model parameter, updated codebase to use Dedupe 2.0, added support for multiprocessing, added gazetteer_dataframe.

Daniel Marczin - Extensive updates to documentation to enhance readability.

Alexis-Evelyn - Fixed logger warning with related to Pandas.

Niels Horn - Cleaned up utility functions.

Credits

Many thanks to folks at DataMade for making the the Dedupe library publicly available. People interested in a code-free implementation of the dedupe library can find a link here: Dedupe.io.