I was trying out this example: https://recordlinkage.readthedocs.io/en/latest/notebooks/data_deduplication.html
Following is the code snippet:
import recordlinkage
from recordlinkage.datasets import load_febrl1
dfA = load_febrl1()
# Indexation step
indexer = recordlinkage.Index()
indexer.block(left_on='given_name')
candidate_links = indexer.index(dfA)
compare_cl = recordlinkage.Compare()
compare_cl.exact('given_name', 'given_name', label='given_name')
compare_cl.string('surname', 'surname', method='jarowinkler', threshold=0.85, label='surname')
compare_cl.exact('date_of_birth', 'date_of_birth', label='date_of_birth')
compare_cl.exact('suburb', 'suburb', label='suburb')
compare_cl.exact('state', 'state', label='state')
compare_cl.string('address_1', 'address_1', threshold=0.85,
label='address_1')
features = compare_cl.compute(candidate_links, dfA)
matches = features[features.sum(axis=1) > 3]
print(len(matches))
I would now like to separately print the record_ids that have been matched.I tried listing down the column names of 'matches', but record_id isn't a part of it, and I cannot seem to figure out a way to get it done(I just want the record_ids separately)
Is there a way to retrieve the record_ids, and maybe either print it separately or store it as a list or an array?
Don't forget that a Pandas data frame has an "index" in addition to its data columns. Usually this is a single "extra" column of integers or strings, but more complex indices are possible, e.g. a "multi-index" consisting of more than one column.
You can see this if you
print(matches.head())
. The first two columns have names that are slightly offset, because they aren't data columns; they are columns in the index itself. This data frame index is in fact a multi-index containing two columns:rec_id_1
andrec_id_2
.The result from
load_febrl
encodes record ID as the index ofdfA
.Compare.compute
preserves the indices of the input data: you can always expect the indices from the original data to be preserved as a multi-index.The index of a data frame by itself can be accessed with the
DataFrame.index
attribute. This returns anIndex
object (of whichMultiIndex
is a subclass) that can in turn be converted as follows:Index.tolist()
: convert to alist
of its elements;MultiIndex
becomes alist
oftuple
sIndex.to_series()
: convert to aSeries
of its elements;MultiIndex
becomes aSeries
oftuple
sIndex.values
: access underlying data as NumPyndarray
;MultiIndex
becomes andarray
oftuple
s.Index.to_frame()
: convert to aDataFrame
, with index columns as data frame columnsSo you can quickly access the record id's with
matches.index
, or export them to a list withmatches.tolist()
.You can also use
matches.reset_index()
to turn Index columns back into regular data columns.