├── data ├── hr.csv └── it.csv ├── pictures ├── battle.jpg ├── giant baby.jpg └── dinoheader_th.jpg ├── README.md ├── LICENSE └── Jupyter notebook [Medium-FuzzyWuzzy].ipynb /data/hr.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kelmouloudi/MediumArticle-FuzzyWuzzy/HEAD/data/hr.csv -------------------------------------------------------------------------------- /pictures/battle.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kelmouloudi/MediumArticle-FuzzyWuzzy/HEAD/pictures/battle.jpg -------------------------------------------------------------------------------- /pictures/giant baby.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kelmouloudi/MediumArticle-FuzzyWuzzy/HEAD/pictures/giant baby.jpg -------------------------------------------------------------------------------- /pictures/dinoheader_th.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kelmouloudi/MediumArticle-FuzzyWuzzy/HEAD/pictures/dinoheader_th.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Matching messy Pandas columns with FuzzyWuzzy 2 | 3 | This is the Github repositoy for the Medium article **Matching messy Pandas columns with FuzzyWuzzy**: https://medium.com/analytics-vidhya/matching-messy-pandas-columns-with-fuzzywuzzy-4adda6c7994f?source=friends_link&sk=e1b211bcfb132973c9c76f3f1d71e32e 4 | 5 | In here, you'll find the **Jupyter Notebook** with all the steps and explanations as well as **the data** used. 6 | -------------------------------------------------------------------------------- /data/it.csv: -------------------------------------------------------------------------------- 1 | email 2 | l.kane@giantbabybibs.org 3 | catkison@giantbabybibs.org 4 | t.raney@giantbabybibs.org 5 | tmichael@giantbabybibs.org 6 | c.foard@giantbabybibs.org 7 | l.aslett@giantbabybibs.org 8 | i.natividad@giantbabybibs.org 9 | a.schwindt@giantbabybibs.org 10 | s.hurstbaumer@giantbabybibs.org 11 | nmarlin@giantbabybibs.org 12 | lane.leo@giantbabybibs.org 13 | s.eadie@giantbabybibs.org 14 | y.brower@giantbabybibs.org 15 | d.charboneau@giantbabybibs.org 16 | g.meidinger@giantbabybibs.org 17 | k.bohannon@giantbabybibs.org 18 | k.doyal@giantbabybibs.org 19 | m.pennywell@giantbabybibs.org 20 | zkilby@giantbabybibs.org 21 | i.paulette@giantbabybibs.org 22 | m.ramer@giantbabybibs.org 23 | t.grayson@giantbabybibs.org 24 | a.drucker@giantbabybibs.org 25 | l.pasha@giantbabybibs.org 26 | h.nickel@giantbabybibs.org 27 | c.rager@giantbabybibs.org 28 | c.p.caruso@giantbabybibs.org 29 | j.willison@giantbabybibs.org 30 | b.stinson@giantbabybibs.org 31 | e.manor@giantbabybibs.org 32 | g.robasil@giantbabybibs.org 33 | r.tooker@giantbabybibs.org 34 | mbrennen@giantbabybibs.org 35 | m.desmarais@giantbabybibs.org 36 | e.lucien@giantbabybibs.org 37 | a.curlingmaloy@giantbabybibs.org 38 | j.colston@giantbabybibs.org 39 | m.sandage@giantbabybibs.org 40 | g.fiala@giantbabybibs.org 41 | d.neagle@giantbabybibs.org 42 | mpershall@giantbabybibs.org 43 | e.bonomo@giantbabybibs.org 44 | i.poison@giantbabybibs.org 45 | h.kistner@giantbabybibs.org 46 | c.kent@giantbabybibs.org 47 | l.renaecusumano@giantbabybibs.org 48 | c.froehlich@giantbabybibs.org 49 | s.harsch@giantbabybibs.org 50 | s.carstarphen@giantbabybibs.org 51 | bbaucom@giantbabybibs.org 52 | m.alvidrez@giantbabybibs.org 53 | l.labounty@giantbabybibs.org 54 | a.laber@giantbabybibs.org 55 | h.lappin@giantbabybibs.org 56 | b.onofrio@giantbabybibs.org 57 | t.fonz@giantbabybibs.org 58 | c.petrick@giantbabybibs.org 59 | tsattler@giantbabybibs.org 60 | r.ethatch@giantbabybibs.org 61 | m.runk@giantbabybibs.org 62 | m.fimbres@giantbabybibs.org 63 | d.wurm@giantbabybibs.org 64 | c.coursey@giantbabybibs.org 65 | slundquist@giantbabybibs.org 66 | t.wiseau@giantbabybibs.org 67 | r.siewert@giantbabybibs.org 68 | m.lundholm@giantbabybibs.org 69 | s.arehart@giantbabybibs.org 70 | k.davila@giantbabybibs.org 71 | f.riviera@giantbabybibs.org 72 | k.brazee@giantbabybibs.org 73 | palmatait@giantbabybibs.org 74 | g.zellars@giantbabybibs.org 75 | h.tubervillewyche@giantbabybibs.org 76 | toshiko@giantbabybibs.org 77 | i.canizales@giantbabybibs.org 78 | d.frenz@giantbabybibs.org 79 | m.schorr@giantbabybibs.org 80 | a.koski@giantbabybibs.org 81 | s.bruemmer@giantbabybibs.org 82 | vmoench@giantbabybibs.org 83 | j.kmjerez@giantbabybibs.org 84 | a.aumiller@giantbabybibs.org 85 | frederickson@giantbabybibs.org 86 | w.farris@giantbabybibs.org 87 | l.bradish@giantbabybibs.org 88 | a.heitz@giantbabybibs.org 89 | r.pascucci@giantbabybibs.org 90 | j.beckler@giantbabybibs.org 91 | w.warden@giantbabybibs.org 92 | a.levesque@giantbabybibs.org 93 | c.schomer@giantbabybibs.org 94 | l.mertens@giantbabybibs.org 95 | b.banner@giantbabybibs.org 96 | d.dolby@giantbabybibs.org 97 | b.tank@giantbabybibs.org 98 | a.bittinger@giantbabybibs.org 99 | sm.breazeale@giantbabybibs.org 100 | c.vanantwerp@giantbabybibs.org 101 | g.shryock@giantbabybibs.org 102 | i.durgin@giantbabybibs.org 103 | d.sieber@giantbabybibs.org 104 | d.leitner@giantbabybibs.org 105 | c.towler@giantbabybibs.org 106 | p.naughton@giantbabybibs.org 107 | d.perfecto@giantbabybibs.org 108 | j.stroop@giantbabybibs.org 109 | b.romine@giantbabybibs.org 110 | m.hippert@giantbabybibs.org 111 | d.caples@giantbabybibs.org 112 | b.boyce@giantbabybibs.org 113 | n.dettman@giantbabybibs.org 114 | l.liss@giantbabybibs.org 115 | a.moctezuma@giantbabybibs.org 116 | r.morrone@giantbabybibs.org 117 | d.kaneshiro@giantbabybibs.org 118 | n.duerr@giantbabybibs.org 119 | l.boller@giantbabybibs.org 120 | j.toro@giantbabybibs.org 121 | r.budd@giantbabybibs.org 122 | b.dipasquale@giantbabybibs.org 123 | b.wayne@giantbabybibs.org 124 | o.uyeda@giantbabybibs.org 125 | f.walczak@giantbabybibs.org 126 | c.hagenbuch@giantbabybibs.org 127 | e.tubman@giantbabybibs.org 128 | d.briley@giantbabybibs.org 129 | r.prost@giantbabybibs.org 130 | m.ramires@giantbabybibs.org 131 | a.marrs@giantbabybibs.org 132 | e.mitts@giantbabybibs.org 133 | w.mcgavock@giantbabybibs.org 134 | t.nicholas@giantbabybibs.org 135 | e.ladwig@giantbabybibs.org 136 | h.falcon@giantbabybibs.org 137 | t.heintz@giantbabybibs.org 138 | l.latch@giantbabybibs.org 139 | m.chupp@giantbabybibs.org 140 | d.zak@giantbabybibs.org 141 | t.staiger@giantbabybibs.org 142 | c.harrold@giantbabybibs.org 143 | kstogner@giantbabybibs.org 144 | blythe.greek@giantbabybibs.org 145 | m.arora@giantbabybibs.org 146 | n.schewe@giantbabybibs.org 147 | e.helfer@giantbabybibs.org 148 | r.chauvin@giantbabybibs.org 149 | s.childress@giantbabybibs.org 150 | s.carman@giantbabybibs.org 151 | r.pondsreali@giantbabybibs.org 152 | mcrafton@giantbabybibs.org 153 | n.tash@giantbabybibs.org 154 | jblick@giantbabybibs.org 155 | g.staton@giantbabybibs.org 156 | m.varnado@giantbabybibs.org 157 | o.reploge@giantbabybibs.org 158 | l.dean@giantbabybibs.org 159 | o.eckles@giantbabybibs.org 160 | c.burgard@giantbabybibs.org 161 | j.kraatz@giantbabybibs.org 162 | m.machen@giantbabybibs.org 163 | m.munroe@giantbabybibs.org 164 | gaitercuffee@giantbabybibs.org 165 | masakoreal@giantbabybibs.org 166 | a.levesque@giantbabybibs.org 167 | s.archer@giantbabybibs.org 168 | l.whittier@giantbabybibs.org 169 | bjones@giantbabybibs.org 170 | l.lukowski@giantbabybibs.org 171 | f.redfern@giantbabybibs.org 172 | lhillman@giantbabybibs.org 173 | s.meiser@giantbabybibs.org 174 | c.ruple@giantbabybibs.org 175 | l.monteiro@giantbabybibs.org 176 | ttowe@giantbabybibs.org 177 | r.may@giantbabybibs.org 178 | k.exner@giantbabybibs.org 179 | f.sutherlin@giantbabybibs.org 180 | a.brinkman@giantbabybibs.org 181 | s.carrillo@giantbabybibs.org 182 | k.demille@giantbabybibs.org 183 | n.romanova@giantbabybibs.org 184 | t.stark@giantbabybibs.org 185 | d.phan@giantbabybibs.org 186 | l.berry@giantbabybibs.org 187 | cstrode@giantbabybibs.org 188 | e.briscoe@giantbabybibs.org 189 | d.schaller@giantbabybibs.org 190 | mtourigny@giantbabybibs.org 191 | s.pearce@giantbabybibs.org 192 | l.pursley@giantbabybibs.org 193 | j.middaugh@giantbabybibs.org 194 | e.liebsch@giantbabybibs.org 195 | r.neifert@giantbabybibs.org 196 | a.dashiell@giantbabybibs.org 197 | d.stansel@giantbabybibs.org 198 | a.kyler@giantbabybibs.org 199 | a.milling@giantbabybibs.org 200 | k.heit@giantbabybibs.org 201 | d.burnell@giantbabybibs.org 202 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Jupyter notebook [Medium-FuzzyWuzzy].ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "\n", 8 | "# Matching messy Pandas columns with FuzzyWuzzy :" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "![header](https://i.ibb.co/wLxk8nK/dinoheader-th.jpg)\n", 16 | "\n", 17 | "In this article, I'm going to show you how to use the Python package **FuzzyWuzzy** to **match two Pandas dataframe columns** based on **string similarity**; the intended outcome is to have each value of column A matched with the closest corresponding value in column B, which is then put in the same row.\n", 18 | "\n", 19 | "To do so, I'm going to use a messy real world case I personally encountered (personal and corporate info are substituted with fictitious and randomly generated data).\n", 20 | "\n", 21 | "Now before tackling our little problem, here's what to expect from this article :\n", 22 | "* Often times, when you work with different data sources, the same entity (business, country, address, phone number, hotel room type, music album, sport team, ...) could be written or worded differently. In this article, we're going to work on matching full names with emails. However, you can apply the same techniques for any string matching problem you encounter (string is just text, simply put) ;

\n", 23 | "\n", 24 | "* You can follow along with me by downloading the **Jupyter Notebook** and **the data** from my [Github repository](https://github.com/kelmouloudi/MediumArticle-FuzzyWuzzy) ;

\n", 25 | "\n", 26 | "* This is a very beginner friendly article. In the Jupyter notebook, I made sure to include an **appendix** towards the end where I explain in as much details as possible every code snippet that may seem complicated (just click the `code explanation` link on the top-right corner of code cells to get to the explanation in the appendix, then hit `go back` to go back to the original cell). \n", 27 | "However, a basic understanding of Python, Jupyter Notebooks and Pandas (which is a Python package used for data handling and analysis, among other things) will help you greatly." 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": {}, 33 | "source": [ 34 | "\n", 35 | "\n", 36 | "---\n", 37 | "# Table of contents :" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "* [Introduction](#intro)
\n", 45 | "* [The problem](#theproblem)
\n", 46 | "* [The packages](#thepackages)
\n", 47 | "* [The data](#thedata)
\n", 48 | "* [The \"dumb but it kinda works\" solution](#thedumb)
\n", 49 | "* [The boring FuzzyWuzzy lecture](#theboring)
\n", 50 | "* [The not so boring FuzzyWuzzy application](#thenotsoboring)
\n", 51 | "* [The audit](#theaudit)
\n", 52 | "* [The quest for the best FuzzyWuzzy scorer](#thequest)
\n", 53 | "* [The battle of the scorers](#thebattle)
\n", 54 | "* [The verdict](#theverdict)
\n", 55 | "* [The appendix (code explanations)](#theappendix)\n", 56 | "* [Parting notes](#parting)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "\n", 64 | "\n", 65 | "---\n", 66 | "[Go back to table of contents ^](#table)\n", 67 | "# The problem :" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "![baby](https://i.ibb.co/jvKJF9C/giant-baby.jpg)\n", 75 | "\n", 76 | "I work at a company called Giant Baby Bibs™ that specializes in making bibs for giant babies. GBB has 200 employees. One day I needed to make a list of all employees and their emails. Big surprise: the HR department has a file of all employee IDs and full names but not emails, and the IT department has a CSV file containing all of the emails but no employee IDs or names whatsoever.\n", 77 | "\n", 78 | "As if that isn't messy enough, the IT department doesn't have a clear established rule to how an email should be derived from the full name of an employee. For example, for an employee called **John Doe**, the email could be j.doe@giantbabybibs.org, or jdoe@giantbabybibs.org or even johndoe@giantbabybibs.org (especially for employees who have very short last names).\n", 79 | "\n", 80 | "Now the real kicker comes with employees who have a middle name. For **John Sebastian Doe** for instance, emails could be jsdoe@giantbabybibs.org or j.sebastian.doe@giantbabybibs.org or j.sebastian@giantbabybibs.org or any variation the webmaster felt was appropriate at the time. Way to go Kevin!" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "\n", 88 | "\n", 89 | "---\n", 90 | "[Go back to table of contents ^](#table)\n", 91 | "# The packages :" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "First things first, let's import `Pandas` :" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 1, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "import pandas as pd" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "If you don't have `FuzzyWuzzy` installed, you can install it by running `pip install fuzzywuzzy[speedup]` inside your Jupyter notebook or from the PIP or Anaconda prompt. If this doesn't work, you should install `FuzzyWuzzy` like this: `pip install fuzzywuzzy` and then install `python-levenshtein` like this: `pip install python-Levenshtein` (in order to speed the matching process).\n", 115 | "\n", 116 | "From `FuzzyWuzzy`, we'll mainly need two modules: `process` and `fuzz` :" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 2, 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [ 125 | "from fuzzywuzzy import process, fuzz" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "Let's also import `matplotlib` for some data visualizations down the road :" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 3, 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "import matplotlib.pyplot as plt" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | " And for good measure, let's add some CSS to style our dataframes for added contrast between the axes and the data itself (If you don't like this style, just delete the cell, restart the kernel and re-run all cells).
\n", 149 | "[code explanation 1](#codex1)\n", 150 | "" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 4, 156 | "metadata": {}, 157 | "outputs": [ 158 | { 159 | "data": { 160 | "text/html": [ 161 | "\n" 163 | ], 164 | "text/plain": [ 165 | "" 166 | ] 167 | }, 168 | "metadata": {}, 169 | "output_type": "display_data" 170 | } 171 | ], 172 | "source": [ 173 | "%%HTML\n", 174 | "" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "This next bit just insures the custom CSS style we just applied stays on when we use `style.background_gradient` later on:" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 5, 188 | "metadata": {}, 189 | "outputs": [], 190 | "source": [ 191 | "th_props = [('background','#3f577c'), ('font-family','monospace'), ('color','white'),('border','3px solid white'),\n", 192 | " ('text-align','left !important')]\n", 193 | "styles = [dict(selector=\"th\", props=th_props)]" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "\n", 201 | "\n", 202 | "---\n", 203 | "[Go back to table of contents ^](#table)\n", 204 | "# The data :" 205 | ] 206 | }, 207 | { 208 | "cell_type": "markdown", 209 | "metadata": {}, 210 | "source": [ 211 | "We have two dataframes or tables we'll need to match here :\n", 212 | "* `hr` that contains employee IDs and full names, as maintained by the HR department ;\n", 213 | "* `it` that contains only the emails extracted by the IT department.\n", 214 | "\n", 215 | "Make sure to change the 'data/hr.csv' and 'data/it.csv' paths to where your data is placed in your machine (don't forget to get the data from this article's [Github repository](https://github.com/kelmouloudi/MediumArticle-FuzzyWuzzy) :" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 6, 221 | "metadata": {}, 222 | "outputs": [ 223 | { 224 | "data": { 225 | "text/html": [ 226 | "
\n", 227 | "\n", 240 | "\n", 241 | " \n", 242 | " \n", 243 | " \n", 244 | " \n", 245 | " \n", 246 | " \n", 247 | " \n", 248 | " \n", 249 | " \n", 250 | " \n", 251 | " \n", 252 | " \n", 253 | " \n", 254 | " \n", 255 | " \n", 256 | " \n", 257 | " \n", 258 | " \n", 259 | " \n", 260 | " \n", 261 | " \n", 262 | " \n", 263 | " \n", 264 | " \n", 265 | "
employee_idfull_name
03CA946A9Kortney Lapierre Brazee
1FFB74DA3Gita Meidinger
268B6478EGeorgia Ro Basil
\n", 266 | "
" 267 | ], 268 | "text/plain": [ 269 | " employee_id full_name\n", 270 | "0 3CA946A9 Kortney Lapierre Brazee\n", 271 | "1 FFB74DA3 Gita Meidinger\n", 272 | "2 68B6478E Georgia Ro Basil" 273 | ] 274 | }, 275 | "metadata": {}, 276 | "output_type": "display_data" 277 | }, 278 | { 279 | "data": { 280 | "text/html": [ 281 | "
\n", 282 | "\n", 295 | "\n", 296 | " \n", 297 | " \n", 298 | " \n", 299 | " \n", 300 | " \n", 301 | " \n", 302 | " \n", 303 | " \n", 304 | " \n", 305 | " \n", 306 | " \n", 307 | " \n", 308 | " \n", 309 | " \n", 310 | " \n", 311 | " \n", 312 | " \n", 313 | " \n", 314 | " \n", 315 | " \n", 316 | "
email
0l.kane@giantbabybibs.org
1catkison@giantbabybibs.org
2t.raney@giantbabybibs.org
\n", 317 | "
" 318 | ], 319 | "text/plain": [ 320 | " email\n", 321 | "0 l.kane@giantbabybibs.org\n", 322 | "1 catkison@giantbabybibs.org\n", 323 | "2 t.raney@giantbabybibs.org" 324 | ] 325 | }, 326 | "metadata": {}, 327 | "output_type": "display_data" 328 | } 329 | ], 330 | "source": [ 331 | "hr = pd.read_csv('data/hr.csv', encoding='unicode_escape')\n", 332 | "it = pd.read_csv('data/it.csv', encoding='unicode_escape')\n", 333 | "\n", 334 | "display( hr.head(3) )\n", 335 | "display( it.head(3) )" 336 | ] 337 | }, 338 | { 339 | "cell_type": "markdown", 340 | "metadata": {}, 341 | "source": [ 342 | "Here, we have 200 employees in the `hr` dataframe and 200 emails in the `it` dataframe. To each employee corresponds a single email, and vice versa." 343 | ] 344 | }, 345 | { 346 | "cell_type": "markdown", 347 | "metadata": {}, 348 | "source": [ 349 | "\n", 350 | "\n", 351 | "---\n", 352 | "[Go back to table of contents ^](#table)\n", 353 | "# The \"dumb but it kinda works\" solution :" 354 | ] 355 | }, 356 | { 357 | "cell_type": "markdown", 358 | "metadata": {}, 359 | "source": [ 360 | "Before working the magic of `FuzzyWuzzy`, I'd like to explore a solution to this problem that I initially went with at the beginning. The idea is to :\n", 361 | "\n", 362 | "1. Automatically generate an email (let's call it `gen_email`) using the first letter of the first name coupled with the last name, and separated with a dot. So, for John Doe, the generated email will be j.doe@giantbabybibs.org. By exploring the list of actual emails, this seems like the dominating trend ;\n", 363 | "2. Compare the `gen_email` column with the actual `email` column and output a value of **True** if the generated email exists in the actual email list, and a value of **False** if it doesn't. The True and False values will be stored in a column called `exists`." 364 | ] 365 | }, 366 | { 367 | "cell_type": "markdown", 368 | "metadata": {}, 369 | "source": [ 370 | "Now, let's make the `gen_email` column within the `hr` dataframe :
\n", 371 | "[code explanation 2](#codex2)\n", 372 | "" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": 7, 378 | "metadata": {}, 379 | "outputs": [ 380 | { 381 | "data": { 382 | "text/html": [ 383 | "
\n", 384 | "\n", 397 | "\n", 398 | " \n", 399 | " \n", 400 | " \n", 401 | " \n", 402 | " \n", 403 | " \n", 404 | " \n", 405 | " \n", 406 | " \n", 407 | " \n", 408 | " \n", 409 | " \n", 410 | " \n", 411 | " \n", 412 | " \n", 413 | " \n", 414 | " \n", 415 | " \n", 416 | " \n", 417 | " \n", 418 | " \n", 419 | " \n", 420 | " \n", 421 | " \n", 422 | " \n", 423 | " \n", 424 | " \n", 425 | " \n", 426 | "
employee_idfull_namegen_email
03CA946A9Kortney Lapierre Brazeek.lapierre@giantbabybibs.org
1FFB74DA3Gita Meidingerg.meidinger@giantbabybibs.org
268B6478EGeorgia Ro Basilg.ro@giantbabybibs.org
\n", 427 | "
" 428 | ], 429 | "text/plain": [ 430 | " employee_id full_name gen_email\n", 431 | "0 3CA946A9 Kortney Lapierre Brazee k.lapierre@giantbabybibs.org\n", 432 | "1 FFB74DA3 Gita Meidinger g.meidinger@giantbabybibs.org\n", 433 | "2 68B6478E Georgia Ro Basil g.ro@giantbabybibs.org" 434 | ] 435 | }, 436 | "execution_count": 7, 437 | "metadata": {}, 438 | "output_type": "execute_result" 439 | } 440 | ], 441 | "source": [ 442 | "hr['gen_email'] = hr['full_name'].str[0] + '.' + hr['full_name'].str.split().str[1] + '@giantbabybibs.org'\n", 443 | "hr['gen_email'] = hr['gen_email'].str.lower()\n", 444 | "hr.head(3)" 445 | ] 446 | }, 447 | { 448 | "cell_type": "markdown", 449 | "metadata": {}, 450 | "source": [ 451 | "Now let's check if the `gen_email` values exist in the actual `email` list. For this, we'll create an additional column called `exists` that accepts **True** if the generated email is correct, and **False** otherwise :
\n", 452 | "[code explanation 3](#codex3)\n", 453 | "" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": 8, 459 | "metadata": {}, 460 | "outputs": [ 461 | { 462 | "data": { 463 | "text/html": [ 464 | "
\n", 465 | "\n", 478 | "\n", 479 | " \n", 480 | " \n", 481 | " \n", 482 | " \n", 483 | " \n", 484 | " \n", 485 | " \n", 486 | " \n", 487 | " \n", 488 | " \n", 489 | " \n", 490 | " \n", 491 | " \n", 492 | " \n", 493 | " \n", 494 | " \n", 495 | " \n", 496 | " \n", 497 | " \n", 498 | " \n", 499 | " \n", 500 | " \n", 501 | " \n", 502 | " \n", 503 | " \n", 504 | " \n", 505 | " \n", 506 | " \n", 507 | " \n", 508 | " \n", 509 | " \n", 510 | " \n", 511 | "
employee_idfull_namegen_emailexists
03CA946A9Kortney Lapierre Brazeek.lapierre@giantbabybibs.orgFalse
1FFB74DA3Gita Meidingerg.meidinger@giantbabybibs.orgTrue
268B6478EGeorgia Ro Basilg.ro@giantbabybibs.orgFalse
\n", 512 | "
" 513 | ], 514 | "text/plain": [ 515 | " employee_id full_name gen_email exists\n", 516 | "0 3CA946A9 Kortney Lapierre Brazee k.lapierre@giantbabybibs.org False\n", 517 | "1 FFB74DA3 Gita Meidinger g.meidinger@giantbabybibs.org True\n", 518 | "2 68B6478E Georgia Ro Basil g.ro@giantbabybibs.org False" 519 | ] 520 | }, 521 | "execution_count": 8, 522 | "metadata": {}, 523 | "output_type": "execute_result" 524 | } 525 | ], 526 | "source": [ 527 | "hr['exists'] = hr.apply(lambda x: x.gen_email in list(it.email), axis=1)\n", 528 | "hr.head(3)" 529 | ] 530 | }, 531 | { 532 | "cell_type": "markdown", 533 | "metadata": {}, 534 | "source": [ 535 | "How many emails did we get right using this method?
\n", 536 | "[code explanation 4](#codex4)\n", 537 | "" 538 | ] 539 | }, 540 | { 541 | "cell_type": "code", 542 | "execution_count": 9, 543 | "metadata": {}, 544 | "outputs": [ 545 | { 546 | "data": { 547 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAtEAAABYCAYAAAAz6as+AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAACNdJREFUeJzt3VuopXUZx/HvzxOaZWmTWSSJNZZlOtl4arQ8FRFoOh0sKrqIhsgrA4mwQOjCikAUERW7KCoQ85B04WEESVNzbBimwRuzE5aHPIbiJM48Xex3as2eNbPXu+a/Z621+35gwVrvfjc8+8ezH5798q61U1VIkiRJGt1eky5AkiRJmjUu0ZIkSVJPLtGSJElSTy7RkiRJUk8u0ZIkSVJPLtGSJElSTy7RkiRJUk8u0ZIkSVJPLtGSJElST7OwRH9y0gUsMebZjlm2Y5btmGVb5tmOWbZjlm2NlecsLNH7T7qAJcY82zHLdsyyHbNsyzzbMct2zLKtsfKchSVakiRJmiou0ZIkSVJPLtGSJElSTy7RkiRJUk8u0ZIkSVJPLtGSJElST/tMuoCFrN606aZbnnlm0mUsGecvW4Z5tmGW7ZhlO2bZlnn2U6efnknXIO0pvZboJG8G7u5eHgZsAf7ZvT6xql5tWJskSZI0lXot0VX1LLACIMmlwEtV9aPBc5IESFVtbVWkJEmSNE2a3BOd5N1JNiW5BlgPHJ7khYGvfz7J9d3ztya5OcnDSR5KcnKLGiRJkqQ9peUbC98H/LiqPgj8fRfnXQn8sKpWAp8Drm9YgyRJkrToWi7Rj1XVuhHOOxu4JskG4Fbg4CQHDJ6QZE13pfrhP994Y8MSJUmSpN3X8tM5Xh54vhUYfIfu/gPPwwJvQqyq64DrAFZv2lQbfGe0JEmSpsiifE5096bC55MsT7IXcP7Al9cCF257kWTFYtQgSZIkLZbF/Gcr3wJuZ+4j8R4fOH4hsCrJxiSPAF9bxBokSZKk5lJVk65hIauBmyddxBJinu2YZTtm2Y5ZtmWe7ZhlO2bZ1lh5+m+/JUmSpJ5coiVJkqSeXKIlSZKknlyiJUmSpJ5coiVJkqSeXKIlSZKknlyiJUmSpJ5coiVJkqSeXKIlSZKknlyiJUmSpJ5coiVJkqSeXKIlSZKknlyiJUmSpJ5coiVJkqSeXKIlSZKknlyiJUmSpJ5coiVJkqSeXKIlSZKknlyiJUmSpJ5coiVJkqSe9pl0AQu6+ns3sf63k65i6Th+FebZyK6yvP727NliJEnSnrTglegkW5JsGHgcsYtzj0iyqWWBkiRJ0rQZ5Ur0K1W1YtErkSRJkmbEWPdEd1ec702yvnt8eMg570/yUHf1emOS5d3xLw0cvzbJ3rv7Q0iSJEl70ihL9AEDt3Lc0h17GvhYVR0PXABcOeT7vg5c0V3FXgk8nuTo7vxV3fEtwBd3+6eQJEmS9qBxb+fYF7gqybZF+Kgh3/cAcEmSdwA3V9WjSc4CPgSsSwJwAHML+XaSrAHWAFz7hXNZ87pRfxxJkiRp8Y37EXcXAU8BxzF3lXm/+SdU1S+Ac4FXgDuSnAkE+ElVrege76mqS4d873VVtbKqVq45deWYJUqSJEmLY9wl+o3AE1W1FfgysMN9zUmOBP5UVVcCtwHHAncDn0lyaHfOIUneOWYNkiRJ0kSMu0RfDXwlyYPM3crx8pBzLgA2JdkAvBf4aVU9AnwHuDPJRuAu4G1j1iBJkiRNxIL3RFfV64cce5S5K8vbfLs7/hfgmO75ZcBlQ773BuCGkSv8xnc/Ddw88vlayGrMsxWzlCTp/5T/9luSJEnqySVakiRJ6sklWpIkSerJJVqSJEnqySVakiRJ6sklWpIkSerJJVqSJEnqaeqX6CTLJl3DUmKe7ZhlO2bZjlm2ZZ7tmGU7ZtnWuHlO/RINrJl0AUuMebZjlu2YZTtm2ZZ5tmOW7ZhlW2PlOQtLtCRJkjRVXKIlSZKknmZhib5u0gUsMebZjlm2Y5btmGVb5tmOWbZjlm2NlWeqqnUhkiRJ0pI2C1eiJUmSpKky1Ut0ksuT3JvkiknXMouSnJTk/i7Dy7tjLya5p3scMukaZ0WSI5I81eV2Z3fs4iT3Jfl5kn0nXeMsSfKJgT58Isl59mY/Sd6eZH2SzUn26Y7tMDOdowubn+Ww2dmdZ48uYEiWO8zO7jzn5wiG5LnD7OzOszcXsJOdaIc+7NObU7tEJzkeOLCqTgP2S3LCpGuaQX8FzuwyPDTJB4A/VNXp3eO5Cdc3a+7qcvt4krcAZ1TVqcBG4LwJ1zZTqur2bX0I/A1Yi73Z13PAWcCDMHxmOkdHtl2WDJ+dYI+OYn6WMDA7AZyfvWyX505mJ9ibo5j/e30a8/qwb29O7RINnML/mmMtcPIEa5lJVfVkVW3uXr4GbAGO7v4K+36STLC8WXRGl91FwInAPd1x+3NMSY4Enqqql7A3e6mqzVX1/MChYTPTOTqC+VnuZHaCPbqgIX0J289OcH6ObCd5zp+dYG8uaMjv9bHs2Ie9enOal+g3Af/qnr8IHDzBWmZakmOBZVX1CLAc+AhzeZ4z0cJmyxPAUcAZwNnASuzPFlYDt3TP7c3dM2xmOkd3w7zZCfboOLabnV2m9uXuG5ydYG+ObNvvNfACuzkzp3mJfgE4qHt+UPdaPXX3Rl0FfBWgqp6ruY9kuRU4ZpK1zZKq+ndVvVxVrwG/Bv6I/dnCOcBtYG82MGxmOkfHNH92gj06jiGz8xjsyxb+OzvB3hzVvN/r3Z6Z07xEP8DcfUAwd+XvwV2cqyG6Nxv9DLi4qp5McmCSvbsvrwIem1x1syXJGwZermJuif5o99r+HEOSw4BXq+pZe7OJYTPTOTqG+bOzO2aPjmHI7HwMWIfzc2yDs7N7bW+OYMjv9bA+7NWbU7tEV9V6YHOSe4GtVfXQpGuaQZ8FTgB+kOQe5u7/WddlejjwywnWNmtOS/L7JPcD/6iq3wG/SXIfsIK5v/7Vz6eAX3XPl2Nv9pJk3yRrgeOAO4B9mTcznaOjGZLlJQzMziSnYI+OZEiW35w/O6vqaZyfI5mfZ5KT2H52gr05qvk70buY14d9e9N/tiJJkiT1NLVXoiVJkqRp5RItSZIk9eQSLUmSJPXkEi1JkiT15BItSZIk9eQSLUmSJPXkEi1JkiT15BItSZIk9fQfzEcWq2as2msAAAAASUVORK5CYII=\n", 548 | "text/plain": [ 549 | "
" 550 | ] 551 | }, 552 | "metadata": { 553 | "needs_background": "light" 554 | }, 555 | "output_type": "display_data" 556 | } 557 | ], 558 | "source": [ 559 | "ax = hr.exists.value_counts().sort_values().plot(kind='barh', color=['tomato', 'c'], figsize=(12,1), width=0.3)\n", 560 | "plt.xlim(0,201), plt.xticks(fontsize=8), ax.set_frame_on(False), plt.grid(color='white', alpha=.4, axis='x') ;" 561 | ] 562 | }, 563 | { 564 | "cell_type": "markdown", 565 | "metadata": {}, 566 | "source": [ 567 | "So, out of 200, it turns out we got **148 emails right** and **52 emails wrong** (a success rate of 74%). Not bad! We could dig deeper with `if` statements covering the other ways an email could have been generated (without the dot, using the middle name instead of the last name, etc.), but let's use FuzzyWuzzy for now." 568 | ] 569 | }, 570 | { 571 | "cell_type": "markdown", 572 | "metadata": {}, 573 | "source": [ 574 | "\n", 575 | "\n", 576 | "---\n", 577 | "[Go back to table of contents ^](#table)\n", 578 | "# The boring FuzzyWuzzy lecture :" 579 | ] 580 | }, 581 | { 582 | "cell_type": "markdown", 583 | "metadata": {}, 584 | "source": [ 585 | "Let's start with a basic intro to `FuzzyWuzzy`. This nifty package compares two strings A and B and outputs a ratio that estimates the distance between them. We are talking here about the **Levenshtein distance**, which is the distance between A and B in terms of how many changes we have to make to the string A in order to transform it into string B. The changes include removing, adding or substituting characters. The fewer the changes we have to make, the more similar A and B are, which results in a higher ratio.\n", 586 | "\n", 587 | "If you remember, we imported both `fuzz` and `process` from the `FuzzWuzzy` package. These are the main two modules we are going to cover in this article :\n", 588 | "\n", 589 | ">* **fuzz.ratio**: it compares two strings and outputs a ratio. For example:

\n", 590 | "fuzz.ratio( \"It's sunny outside!\", \"Outside is really sunny\")
\n", 591 | "Output: 38\n", 592 | "\n", 593 | ">* **process.extract**: it takes a string A and then looks for the best matches for it in a list of strings, then returns these strings along with their ratios (the limit parameter tells the module how many similar strings we want to return). For example :

\n", 594 | "list_of_strings = ['a', 'ab', 'c', 'cd']
\n", 595 | "process.extract( 'aaa', list_of_strings, limit=2 )
\n", 596 | "Output: [('a', 90), ('ab', 45)]\n", 597 | "\n", 598 | ">These two modules can use a variety of ways to calculate a ratio, from the basic Levenshtein ratio to the more sophisticated weighted ratio. These ways are called **scorers** and here's how to use them :

\n", 599 | "* fuzz.ratio(...)\n", 600 | "* fuzz.partial_ratio(...)\n", 601 | "* fuzz.WRatio(...) etc.\n", 602 | "* process.extract( ..., scorer=fuzz.ratio)\n", 603 | "* process.extract( ..., scorer=fuzz.partial_ratio)\n", 604 | "* process.extract( ..., scorer=fuzz.WRatio), etc.

\n", 605 | "We'll see the complete list of scorers and what each of them does later on. Ok, enough with the theory, let's get to the practice!" 606 | ] 607 | }, 608 | { 609 | "cell_type": "markdown", 610 | "metadata": {}, 611 | "source": [ 612 | "\n", 613 | "\n", 614 | "---\n", 615 | "[Go back to table of contents ^](#table)\n", 616 | "# The not so boring FuzzyWuzzy application :" 617 | ] 618 | }, 619 | { 620 | "cell_type": "markdown", 621 | "metadata": {}, 622 | "source": [ 623 | "In our case, we want to compare each full name in the `hr.full_name` column with all the actual emails in the `it.email` column, so we'll be using **process.extract**. \n", 624 | "\n", 625 | "However, we only need the first part of the emails for this comparison, because the domain name part `@giantbabybibs.org` could interfere with ratio calculation. Let's generate a new column within the `it` dataframe called `username` :
\n", 626 | "[code explanation 5](#codex5)\n", 627 | "" 628 | ] 629 | }, 630 | { 631 | "cell_type": "code", 632 | "execution_count": 10, 633 | "metadata": {}, 634 | "outputs": [ 635 | { 636 | "data": { 637 | "text/html": [ 638 | "
\n", 639 | "\n", 652 | "\n", 653 | " \n", 654 | " \n", 655 | " \n", 656 | " \n", 657 | " \n", 658 | " \n", 659 | " \n", 660 | " \n", 661 | " \n", 662 | " \n", 663 | " \n", 664 | " \n", 665 | " \n", 666 | " \n", 667 | " \n", 668 | " \n", 669 | " \n", 670 | " \n", 671 | " \n", 672 | " \n", 673 | " \n", 674 | " \n", 675 | " \n", 676 | " \n", 677 | "
emailusername
0l.kane@giantbabybibs.orgl.kane
1catkison@giantbabybibs.orgcatkison
2t.raney@giantbabybibs.orgt.raney
\n", 678 | "
" 679 | ], 680 | "text/plain": [ 681 | " email username\n", 682 | "0 l.kane@giantbabybibs.org l.kane\n", 683 | "1 catkison@giantbabybibs.org catkison\n", 684 | "2 t.raney@giantbabybibs.org t.raney" 685 | ] 686 | }, 687 | "execution_count": 10, 688 | "metadata": {}, 689 | "output_type": "execute_result" 690 | } 691 | ], 692 | "source": [ 693 | "it['username'] = it['email'].str.replace('@giantbabybibs.org', '')\n", 694 | "it.head(3)" 695 | ] 696 | }, 697 | { 698 | "cell_type": "markdown", 699 | "metadata": {}, 700 | "source": [ 701 | "Now let's use **process.extract** to compare `hr.full_name` to `it.username` (note that we added the domaine name `@giantbabybibs.org` AFTER calculating the ratios):
\n", 702 | "[code explanation 6](#codex6)\n", 703 | "" 704 | ] 705 | }, 706 | { 707 | "cell_type": "code", 708 | "execution_count": 11, 709 | "metadata": {}, 710 | "outputs": [ 711 | { 712 | "data": { 713 | "text/html": [ 714 | "
\n", 715 | "\n", 728 | "\n", 729 | " \n", 730 | " \n", 731 | " \n", 732 | " \n", 733 | " \n", 734 | " \n", 735 | " \n", 736 | " \n", 737 | " \n", 738 | " \n", 739 | " \n", 740 | " \n", 741 | " \n", 742 | " \n", 743 | " \n", 744 | " \n", 745 | " \n", 746 | " \n", 747 | " \n", 748 | " \n", 749 | " \n", 750 | " \n", 751 | " \n", 752 | " \n", 753 | " \n", 754 | " \n", 755 | " \n", 756 | " \n", 757 | " \n", 758 | " \n", 759 | " \n", 760 | " \n", 761 | " \n", 762 | " \n", 763 | " \n", 764 | " \n", 765 | " \n", 766 | " \n", 767 | " \n", 768 | " \n", 769 | "
employee_idfull_namegen_emailexistsactual_emailsimilarity
03CA946A9Kortney Lapierre Brazeek.lapierre@giantbabybibs.orgFalsek.brazee@giantbabybibs.org86
1FFB74DA3Gita Meidingerg.meidinger@giantbabybibs.orgTrueg.meidinger@giantbabybibs.org88
268B6478EGeorgia Ro Basilg.ro@giantbabybibs.orgFalseg.robasil@giantbabybibs.org80
\n", 770 | "
" 771 | ], 772 | "text/plain": [ 773 | " employee_id full_name gen_email exists \\\n", 774 | "0 3CA946A9 Kortney Lapierre Brazee k.lapierre@giantbabybibs.org False \n", 775 | "1 FFB74DA3 Gita Meidinger g.meidinger@giantbabybibs.org True \n", 776 | "2 68B6478E Georgia Ro Basil g.ro@giantbabybibs.org False \n", 777 | "\n", 778 | " actual_email similarity \n", 779 | "0 k.brazee@giantbabybibs.org 86 \n", 780 | "1 g.meidinger@giantbabybibs.org 88 \n", 781 | "2 g.robasil@giantbabybibs.org 80 " 782 | ] 783 | }, 784 | "execution_count": 11, 785 | "metadata": {}, 786 | "output_type": "execute_result" 787 | } 788 | ], 789 | "source": [ 790 | "actual_email = []\n", 791 | "similarity = []\n", 792 | "\n", 793 | "for i in hr.full_name:\n", 794 | " ratio = process.extract( i, it.username, limit=1)\n", 795 | " actual_email.append(ratio[0][0])\n", 796 | " similarity.append(ratio[0][1])\n", 797 | "\n", 798 | "hr['actual_email'] = pd.Series(actual_email)\n", 799 | "hr['actual_email'] = hr['actual_email'] + '@giantbabybibs.org'\n", 800 | "hr['similarity'] = pd.Series(similarity)\n", 801 | "\n", 802 | "hr.head(3)" 803 | ] 804 | }, 805 | { 806 | "cell_type": "markdown", 807 | "metadata": {}, 808 | "source": [ 809 | "The Fuzz Wizard has worked its magic! Now let's simplify this dataframe by only keeping the `full_name`, `actual_email` and `similarity` columns. Let's call it `final_result` :
\n", 810 | "[code explanation 7](#codex7)\n", 811 | "" 812 | ] 813 | }, 814 | { 815 | "cell_type": "code", 816 | "execution_count": 12, 817 | "metadata": {}, 818 | "outputs": [ 819 | { 820 | "data": { 821 | "text/html": [ 822 | "
\n", 823 | "\n", 836 | "\n", 837 | " \n", 838 | " \n", 839 | " \n", 840 | " \n", 841 | " \n", 842 | " \n", 843 | " \n", 844 | " \n", 845 | " \n", 846 | " \n", 847 | " \n", 848 | " \n", 849 | " \n", 850 | " \n", 851 | " \n", 852 | " \n", 853 | " \n", 854 | " \n", 855 | " \n", 856 | " \n", 857 | " \n", 858 | " \n", 859 | " \n", 860 | " \n", 861 | " \n", 862 | " \n", 863 | " \n", 864 | " \n", 865 | "
full_nameactual_emailsimilarity
0Kortney Lapierre Brazeek.brazee@giantbabybibs.org86
1Gita Meidingerg.meidinger@giantbabybibs.org88
2Georgia Ro Basilg.robasil@giantbabybibs.org80
\n", 866 | "
" 867 | ], 868 | "text/plain": [ 869 | " full_name actual_email similarity\n", 870 | "0 Kortney Lapierre Brazee k.brazee@giantbabybibs.org 86\n", 871 | "1 Gita Meidinger g.meidinger@giantbabybibs.org 88\n", 872 | "2 Georgia Ro Basil g.robasil@giantbabybibs.org 80" 873 | ] 874 | }, 875 | "execution_count": 12, 876 | "metadata": {}, 877 | "output_type": "execute_result" 878 | } 879 | ], 880 | "source": [ 881 | "final_result = hr[['full_name', 'actual_email', 'similarity']]\n", 882 | "final_result.head(3)" 883 | ] 884 | }, 885 | { 886 | "cell_type": "markdown", 887 | "metadata": {}, 888 | "source": [ 889 | "\n", 890 | "\n", 891 | "---\n", 892 | "[Go back to table of contents ^](#table)\n", 893 | "# The audit :" 894 | ] 895 | }, 896 | { 897 | "cell_type": "markdown", 898 | "metadata": {}, 899 | "source": [ 900 | "This is good and all, but how can we make sure every employee is matched to the correct email? A good way to do so is to format the dataframe so the low ratios would be easier to spot and inspect using a colormap. In our case, we're going to use the reversed summer colormap `summer_r` where high ratios get colored green and low ratios take the color yellow :
\n", 901 | "[code explanation 8](#codex8)\n", 902 | "" 903 | ] 904 | }, 905 | { 906 | "cell_type": "code", 907 | "execution_count": 13, 908 | "metadata": { 909 | "scrolled": false 910 | }, 911 | "outputs": [ 912 | { 913 | "data": { 914 | "text/html": [ 915 | "\n", 947 | " \n", 948 | " \n", 949 | " \n", 950 | " \n", 951 | " \n", 952 | " \n", 953 | " \n", 954 | " \n", 955 | " \n", 956 | " \n", 957 | " \n", 958 | " \n", 959 | " \n", 960 | " \n", 961 | " \n", 962 | " \n", 963 | " \n", 964 | " \n", 965 | " \n", 966 | " \n", 967 | " \n", 968 | " \n", 969 | " \n", 970 | " \n", 971 | " \n", 972 | " \n", 973 | " \n", 974 | " \n", 975 | " \n", 976 | " \n", 977 | " \n", 978 | " \n", 979 | " \n", 980 | " \n", 981 | " \n", 982 | " \n", 983 | " \n", 984 | " \n", 985 | " \n", 986 | " \n", 987 | " \n", 988 | " \n", 989 | " \n", 990 | " \n", 991 | " \n", 992 | " \n", 993 | " \n", 994 | " \n", 995 | "
full_name actual_email similarity
0Kortney Lapierre Brazeek.brazee@giantbabybibs.org86
1Gita Meidingerg.meidinger@giantbabybibs.org88
2Georgia Ro Basilg.robasil@giantbabybibs.org80
3Consuela Schomerc.schomer@giantbabybibs.org86
4Domingo Stanseld.stansel@giantbabybibs.org86
5Avelina Brinkmana.brinkman@giantbabybibs.org90
6Felipa Rivieraf.riviera@giantbabybibs.org86
7Nevada Deane Duerrn.duerr@giantbabybibs.org86
" 996 | ], 997 | "text/plain": [ 998 | "" 999 | ] 1000 | }, 1001 | "execution_count": 13, 1002 | "metadata": {}, 1003 | "output_type": "execute_result" 1004 | } 1005 | ], 1006 | "source": [ 1007 | "final_result.head(8).style.background_gradient(subset='similarity', cmap='summer_r') \\\n", 1008 | " .set_table_styles(styles)" 1009 | ] 1010 | }, 1011 | { 1012 | "cell_type": "markdown", 1013 | "metadata": {}, 1014 | "source": [ 1015 | "In addition to this, we can sort the `final_result` dataframe by `similarity` and then inspect the low ratios (note that a high ratio does not mean the matching is correct. The reverse is also true) :" 1016 | ] 1017 | }, 1018 | { 1019 | "cell_type": "code", 1020 | "execution_count": 14, 1021 | "metadata": {}, 1022 | "outputs": [ 1023 | { 1024 | "data": { 1025 | "text/html": [ 1026 | "
\n", 1027 | "\n", 1040 | "\n", 1041 | " \n", 1042 | " \n", 1043 | " \n", 1044 | " \n", 1045 | " \n", 1046 | " \n", 1047 | " \n", 1048 | " \n", 1049 | " \n", 1050 | " \n", 1051 | " \n", 1052 | " \n", 1053 | " \n", 1054 | " \n", 1055 | " \n", 1056 | " \n", 1057 | " \n", 1058 | " \n", 1059 | " \n", 1060 | " \n", 1061 | " \n", 1062 | " \n", 1063 | " \n", 1064 | " \n", 1065 | " \n", 1066 | " \n", 1067 | " \n", 1068 | " \n", 1069 | " \n", 1070 | " \n", 1071 | " \n", 1072 | " \n", 1073 | " \n", 1074 | " \n", 1075 | " \n", 1076 | " \n", 1077 | " \n", 1078 | " \n", 1079 | " \n", 1080 | " \n", 1081 | " \n", 1082 | " \n", 1083 | " \n", 1084 | " \n", 1085 | " \n", 1086 | " \n", 1087 | " \n", 1088 | " \n", 1089 | " \n", 1090 | " \n", 1091 | " \n", 1092 | " \n", 1093 | " \n", 1094 | " \n", 1095 | " \n", 1096 | " \n", 1097 | " \n", 1098 | " \n", 1099 | "
full_nameactual_emailsimilarity
162Jacquline Karly Mackay Jerezj.kmjerez@giantbabybibs.org60
94Tammi Towettowe@giantbabybibs.org72
167Barnaby Jonesbjones@giantbabybibs.org75
115Zenaida Kilbyzkilby@giantbabybibs.org75
193Weston Farisw.farris@giantbabybibs.org75
12Velvet Moenchvmoench@giantbabybibs.org77
165Beulah Baucombbaucom@giantbabybibs.org77
178Curtis Strodecstrode@giantbabybibs.org77
\n", 1100 | "
" 1101 | ], 1102 | "text/plain": [ 1103 | " full_name actual_email similarity\n", 1104 | "162 Jacquline Karly Mackay Jerez j.kmjerez@giantbabybibs.org 60\n", 1105 | "94 Tammi Towe ttowe@giantbabybibs.org 72\n", 1106 | "167 Barnaby Jones bjones@giantbabybibs.org 75\n", 1107 | "115 Zenaida Kilby zkilby@giantbabybibs.org 75\n", 1108 | "193 Weston Faris w.farris@giantbabybibs.org 75\n", 1109 | "12 Velvet Moench vmoench@giantbabybibs.org 77\n", 1110 | "165 Beulah Baucom bbaucom@giantbabybibs.org 77\n", 1111 | "178 Curtis Strode cstrode@giantbabybibs.org 77" 1112 | ] 1113 | }, 1114 | "execution_count": 14, 1115 | "metadata": {}, 1116 | "output_type": "execute_result" 1117 | } 1118 | ], 1119 | "source": [ 1120 | "final_result.sort_values(by='similarity').head(8)" 1121 | ] 1122 | }, 1123 | { 1124 | "cell_type": "markdown", 1125 | "metadata": {}, 1126 | "source": [ 1127 | "But, what can you do if your dataframe is huge (say a million records or more)? You can use **random sampling** in order to control whether the matching was successful or not. You should run the code below as many times as you need, because each time you do you'll get a new random sample to inspect (the `random_state=9` parameter here insure you'll get the same sample as I did. Remove it in order to get a random sample every time you run the cell) :" 1128 | ] 1129 | }, 1130 | { 1131 | "cell_type": "code", 1132 | "execution_count": 15, 1133 | "metadata": {}, 1134 | "outputs": [ 1135 | { 1136 | "data": { 1137 | "text/html": [ 1138 | "
\n", 1139 | "\n", 1152 | "\n", 1153 | " \n", 1154 | " \n", 1155 | " \n", 1156 | " \n", 1157 | " \n", 1158 | " \n", 1159 | " \n", 1160 | " \n", 1161 | " \n", 1162 | " \n", 1163 | " \n", 1164 | " \n", 1165 | " \n", 1166 | " \n", 1167 | " \n", 1168 | " \n", 1169 | " \n", 1170 | " \n", 1171 | " \n", 1172 | " \n", 1173 | " \n", 1174 | " \n", 1175 | " \n", 1176 | " \n", 1177 | " \n", 1178 | " \n", 1179 | " \n", 1180 | " \n", 1181 | " \n", 1182 | " \n", 1183 | " \n", 1184 | " \n", 1185 | " \n", 1186 | " \n", 1187 | " \n", 1188 | " \n", 1189 | " \n", 1190 | " \n", 1191 | " \n", 1192 | " \n", 1193 | " \n", 1194 | " \n", 1195 | " \n", 1196 | " \n", 1197 | " \n", 1198 | " \n", 1199 | " \n", 1200 | " \n", 1201 | " \n", 1202 | " \n", 1203 | " \n", 1204 | " \n", 1205 | " \n", 1206 | " \n", 1207 | " \n", 1208 | " \n", 1209 | " \n", 1210 | " \n", 1211 | "
full_nameactual_emailsimilarity
84Saran Mackay Breazealesm.breazeale@giantbabybibs.org86
60Rocio E. Thatche.manor@giantbabybibs.org86
54Awilda Kylera.kyler@giantbabybibs.org90
109Hilda Falconh.falcon@giantbabybibs.org86
156Tomi Heintzt.heintz@giantbabybibs.org84
140Darlene Frenzd.frenz@giantbabybibs.org86
39Lane Leolane.leo@giantbabybibs.org100
116Debra Leitnerd.leitner@giantbabybibs.org84
\n", 1212 | "
" 1213 | ], 1214 | "text/plain": [ 1215 | " full_name actual_email similarity\n", 1216 | "84 Saran Mackay Breazeale sm.breazeale@giantbabybibs.org 86\n", 1217 | "60 Rocio E. Thatch e.manor@giantbabybibs.org 86\n", 1218 | "54 Awilda Kyler a.kyler@giantbabybibs.org 90\n", 1219 | "109 Hilda Falcon h.falcon@giantbabybibs.org 86\n", 1220 | "156 Tomi Heintz t.heintz@giantbabybibs.org 84\n", 1221 | "140 Darlene Frenz d.frenz@giantbabybibs.org 86\n", 1222 | "39 Lane Leo lane.leo@giantbabybibs.org 100\n", 1223 | "116 Debra Leitner d.leitner@giantbabybibs.org 84" 1224 | ] 1225 | }, 1226 | "execution_count": 15, 1227 | "metadata": {}, 1228 | "output_type": "execute_result" 1229 | } 1230 | ], 1231 | "source": [ 1232 | "sample = final_result.sample( n=8, random_state=9 )\n", 1233 | "sample" 1234 | ] 1235 | }, 1236 | { 1237 | "cell_type": "markdown", 1238 | "metadata": {}, 1239 | "source": [ 1240 | "As we can see here, the random sample above shows that one particular mistake was made for employee **N°60: Rocio E. Thatch**. Could that be because of the abbreviated middle name? Let's check if we have any other employees like this one :
\n", 1241 | "[code explanation 9](#codex9)\n", 1242 | "" 1243 | ] 1244 | }, 1245 | { 1246 | "cell_type": "code", 1247 | "execution_count": 16, 1248 | "metadata": {}, 1249 | "outputs": [ 1250 | { 1251 | "data": { 1252 | "text/html": [ 1253 | "
\n", 1254 | "\n", 1267 | "\n", 1268 | " \n", 1269 | " \n", 1270 | " \n", 1271 | " \n", 1272 | " \n", 1273 | " \n", 1274 | " \n", 1275 | " \n", 1276 | " \n", 1277 | " \n", 1278 | " \n", 1279 | " \n", 1280 | " \n", 1281 | " \n", 1282 | " \n", 1283 | " \n", 1284 | " \n", 1285 | " \n", 1286 | " \n", 1287 | " \n", 1288 | " \n", 1289 | " \n", 1290 | " \n", 1291 | " \n", 1292 | " \n", 1293 | " \n", 1294 | " \n", 1295 | " \n", 1296 | " \n", 1297 | " \n", 1298 | " \n", 1299 | " \n", 1300 | " \n", 1301 | " \n", 1302 | "
full_nameactual_emailsimilarity
44Lenora K. Monteirok.bohannon@giantbabybibs.org86
60Rocio E. Thatche.manor@giantbabybibs.org86
70M. Hailey Craftonm.pennywell@giantbabybibs.org86
150Latonya F. Latchf.riviera@giantbabybibs.org86
\n", 1303 | "
" 1304 | ], 1305 | "text/plain": [ 1306 | " full_name actual_email similarity\n", 1307 | "44 Lenora K. Monteiro k.bohannon@giantbabybibs.org 86\n", 1308 | "60 Rocio E. Thatch e.manor@giantbabybibs.org 86\n", 1309 | "70 M. Hailey Crafton m.pennywell@giantbabybibs.org 86\n", 1310 | "150 Latonya F. Latch f.riviera@giantbabybibs.org 86" 1311 | ] 1312 | }, 1313 | "execution_count": 16, 1314 | "metadata": {}, 1315 | "output_type": "execute_result" 1316 | } 1317 | ], 1318 | "source": [ 1319 | "final_result[final_result['full_name'].str.contains('.', regex=False)]" 1320 | ] 1321 | }, 1322 | { 1323 | "cell_type": "markdown", 1324 | "metadata": {}, 1325 | "source": [ 1326 | "Now would you look at that! The process got all of the 4 employees wrong. Even though that's only **4 wrongs** out of **200**, which means a **success rate of 98%**, imagine if you have 173.000 employees in the dataframe (for a company like General Motors for example), that would mean **3460 wrong emails**.\n", 1327 | "\n", 1328 | "To understand why this mistake was made, we have to inspect which `FuzzyWuzzy` **scorer** the process used. A scorer is a method that generates a ratio by comparing two strings. And yes, as we have seen before, `FuzzyWuzzy` has many different scorers you can choose from depending on the kind of data you're working with. Here is a list of said scorers :\n", 1329 | "* fuzz.ratio\n", 1330 | "* fuzz.partial_ratio\n", 1331 | "* fuzz.token_set_ratio\n", 1332 | "* fuzz.token_sort_ratio\n", 1333 | "* fuzz.partial_token_set_ratio\n", 1334 | "* fuzz.partial_token_sort_ratio\n", 1335 | "* **fuzz.WRatio**\n", 1336 | "* fuzz.QRatio\n", 1337 | "* fuzz.UWRatio\n", 1338 | "* fuzz.UQRatio\n", 1339 | "\n", 1340 | "The one used by default for the process is `fuzz.WRatio`. If you want to use `process` with one of these scorers, just pass it as an argument like this:\n", 1341 | "\n", 1342 | "`ratio = process.extract( column_A, column_B, limit=1, scorer=fuzz.ratio )`\n", 1343 | "\n", 1344 | "Next, we'll test some of these scorers to see which one works perfectly in our case. Let the hunt for a success rate of 100% begin!" 1345 | ] 1346 | }, 1347 | { 1348 | "cell_type": "markdown", 1349 | "metadata": {}, 1350 | "source": [ 1351 | "\n", 1352 | "\n", 1353 | "---\n", 1354 | "[Go back to table of contents ^](#table)\n", 1355 | "# The quest for the perfect FuzzyWuzzy scorer :" 1356 | ] 1357 | }, 1358 | { 1359 | "cell_type": "markdown", 1360 | "metadata": {}, 1361 | "source": [ 1362 | "Let's see all the `FuzzyWuzzy` scorers we can use. We'll assign an abbreviation to each of them to use it later as a key in a dictionary (the scorer methods are case sensitive. fuzz.wratio for example will not work) :\n", 1363 | "* fuzz.ratio (R)\n", 1364 | "* fuzz.partial_ratio (PR)\n", 1365 | "* fuzz.token_set_ratio (TSeR)\n", 1366 | "* fuzz.token_sort_ratio (TSoR)\n", 1367 | "* fuzz.partial_token_set_ratio (PTSeR)\n", 1368 | "* fuzz.partial_token_sort_ratio (PTsoR)\n", 1369 | "* fuzz.WRatio (WR)\n", 1370 | "* fuzz.QRatio (QR)\n", 1371 | "* fuzz.UWRatio (UWR)\n", 1372 | "* fuzz.UQRatio (UQR)" 1373 | ] 1374 | }, 1375 | { 1376 | "cell_type": "markdown", 1377 | "metadata": {}, 1378 | "source": [ 1379 | "And here's our fancy `scorer_dict` dictionary :" 1380 | ] 1381 | }, 1382 | { 1383 | "cell_type": "code", 1384 | "execution_count": 17, 1385 | "metadata": {}, 1386 | "outputs": [], 1387 | "source": [ 1388 | "scorer_dict = { 'R':fuzz.ratio, 'PR': fuzz.partial_ratio, 'TSeR': fuzz.token_set_ratio, 'TSoR': fuzz.token_sort_ratio,\n", 1389 | " 'PTSeR': fuzz.partial_token_set_ratio, 'PTSoR': fuzz.partial_token_sort_ratio, 'WR': fuzz.WRatio, \n", 1390 | " 'QR': fuzz.QRatio, 'UWR': fuzz.UWRatio, 'UQR': fuzz.UQRatio }" 1391 | ] 1392 | }, 1393 | { 1394 | "cell_type": "markdown", 1395 | "metadata": {}, 1396 | "source": [ 1397 | "Remember the `hr` dataframe? We'll use its column `full_name` for this test. We'll call it `scorer_test` :" 1398 | ] 1399 | }, 1400 | { 1401 | "cell_type": "code", 1402 | "execution_count": 18, 1403 | "metadata": {}, 1404 | "outputs": [ 1405 | { 1406 | "data": { 1407 | "text/html": [ 1408 | "
\n", 1409 | "\n", 1422 | "\n", 1423 | " \n", 1424 | " \n", 1425 | " \n", 1426 | " \n", 1427 | " \n", 1428 | " \n", 1429 | " \n", 1430 | " \n", 1431 | " \n", 1432 | " \n", 1433 | " \n", 1434 | " \n", 1435 | " \n", 1436 | " \n", 1437 | " \n", 1438 | " \n", 1439 | " \n", 1440 | " \n", 1441 | " \n", 1442 | " \n", 1443 | "
full_name
0Kortney Lapierre Brazee
1Gita Meidinger
2Georgia Ro Basil
\n", 1444 | "
" 1445 | ], 1446 | "text/plain": [ 1447 | " full_name\n", 1448 | "0 Kortney Lapierre Brazee\n", 1449 | "1 Gita Meidinger\n", 1450 | "2 Georgia Ro Basil" 1451 | ] 1452 | }, 1453 | "execution_count": 18, 1454 | "metadata": {}, 1455 | "output_type": "execute_result" 1456 | } 1457 | ], 1458 | "source": [ 1459 | "scorer_test = hr[['full_name']].copy()\n", 1460 | "scorer_test.head(3)" 1461 | ] 1462 | }, 1463 | { 1464 | "cell_type": "markdown", 1465 | "metadata": {}, 1466 | "source": [ 1467 | "And now for the big show! We'll define a **function** called `scorer_tester_function` that takes a **parameter x** from the `scorer_dict` (which is simply the name of the scorer) and matches the `scorer_test.full_name` column with the `it.email` column :
\n", 1468 | "[code explanation 10](#codex10)\n", 1469 | "" 1470 | ] 1471 | }, 1472 | { 1473 | "cell_type": "code", 1474 | "execution_count": 19, 1475 | "metadata": {}, 1476 | "outputs": [], 1477 | "source": [ 1478 | "def scorer_tester_function(x) :\n", 1479 | " actual_email = []\n", 1480 | " similarity = []\n", 1481 | " \n", 1482 | " for i in scorer_test['full_name']:\n", 1483 | " ratio = process.extract( i, it.username, limit=1, scorer=scorer_dict[x])\n", 1484 | " actual_email.append( ratio[0][0] )\n", 1485 | " similarity.append( ratio[0][1] )\n", 1486 | "\n", 1487 | " scorer_test['actual_email'] = pd.Series(actual_email)\n", 1488 | " scorer_test['actual_email'] = scorer_test['actual_email'] + '@giantbabybibs.org'\n", 1489 | " scorer_test['similarity'] = pd.Series(similarity)\n", 1490 | " \n", 1491 | " return scorer_test" 1492 | ] 1493 | }, 1494 | { 1495 | "cell_type": "markdown", 1496 | "metadata": {}, 1497 | "source": [ 1498 | "And then, we'll apply the function with different scorers from the `scorer_dict` dictionary. Let's start with the 4 employees **fuzz.WRatio (WR)** got wrong :

\n", 1499 | "[code explanation 11](#codex11)\n", 1500 | "" 1501 | ] 1502 | }, 1503 | { 1504 | "cell_type": "code", 1505 | "execution_count": 20, 1506 | "metadata": {}, 1507 | "outputs": [ 1508 | { 1509 | "data": { 1510 | "text/html": [ 1511 | "
\n", 1512 | "\n", 1525 | "\n", 1526 | " \n", 1527 | " \n", 1528 | " \n", 1529 | " \n", 1530 | " \n", 1531 | " \n", 1532 | " \n", 1533 | " \n", 1534 | " \n", 1535 | " \n", 1536 | " \n", 1537 | " \n", 1538 | " \n", 1539 | " \n", 1540 | " \n", 1541 | " \n", 1542 | " \n", 1543 | " \n", 1544 | " \n", 1545 | " \n", 1546 | " \n", 1547 | " \n", 1548 | " \n", 1549 | " \n", 1550 | " \n", 1551 | " \n", 1552 | " \n", 1553 | " \n", 1554 | " \n", 1555 | " \n", 1556 | " \n", 1557 | " \n", 1558 | " \n", 1559 | " \n", 1560 | "
full_nameactual_emailsimilarity
44Lenora K. Monteirok.bohannon@giantbabybibs.org86
60Rocio E. Thatche.manor@giantbabybibs.org86
70M. Hailey Craftonm.pennywell@giantbabybibs.org86
150Latonya F. Latchf.riviera@giantbabybibs.org86
\n", 1561 | "
" 1562 | ], 1563 | "text/plain": [ 1564 | " full_name actual_email similarity\n", 1565 | "44 Lenora K. Monteiro k.bohannon@giantbabybibs.org 86\n", 1566 | "60 Rocio E. Thatch e.manor@giantbabybibs.org 86\n", 1567 | "70 M. Hailey Crafton m.pennywell@giantbabybibs.org 86\n", 1568 | "150 Latonya F. Latch f.riviera@giantbabybibs.org 86" 1569 | ] 1570 | }, 1571 | "execution_count": 20, 1572 | "metadata": {}, 1573 | "output_type": "execute_result" 1574 | } 1575 | ], 1576 | "source": [ 1577 | "scorer_tester_function('WR')\n", 1578 | "scorer_test[scorer_test.full_name.str.contains('.', regex=False)]" 1579 | ] 1580 | }, 1581 | { 1582 | "cell_type": "markdown", 1583 | "metadata": {}, 1584 | "source": [ 1585 | "As we can see, **fuzz.WRatio (WR)** performs badly with full names having an abbreviated middle name. Let's see if other scorers will correct this, **fuzz.ratio (R)** for example :" 1586 | ] 1587 | }, 1588 | { 1589 | "cell_type": "code", 1590 | "execution_count": 21, 1591 | "metadata": { 1592 | "scrolled": false 1593 | }, 1594 | "outputs": [ 1595 | { 1596 | "data": { 1597 | "text/html": [ 1598 | "
\n", 1599 | "\n", 1612 | "\n", 1613 | " \n", 1614 | " \n", 1615 | " \n", 1616 | " \n", 1617 | " \n", 1618 | " \n", 1619 | " \n", 1620 | " \n", 1621 | " \n", 1622 | " \n", 1623 | " \n", 1624 | " \n", 1625 | " \n", 1626 | " \n", 1627 | " \n", 1628 | " \n", 1629 | " \n", 1630 | " \n", 1631 | " \n", 1632 | " \n", 1633 | " \n", 1634 | " \n", 1635 | " \n", 1636 | " \n", 1637 | " \n", 1638 | " \n", 1639 | " \n", 1640 | " \n", 1641 | " \n", 1642 | " \n", 1643 | " \n", 1644 | " \n", 1645 | " \n", 1646 | " \n", 1647 | "
full_nameactual_emailsimilarity
44Lenora K. Monteirol.monteiro@giantbabybibs.org71
60Rocio E. Thatchr.ethatch@giantbabybibs.org75
70M. Hailey Craftonmcrafton@giantbabybibs.org64
150Latonya F. Latchl.latch@giantbabybibs.org61
\n", 1648 | "
" 1649 | ], 1650 | "text/plain": [ 1651 | " full_name actual_email similarity\n", 1652 | "44 Lenora K. Monteiro l.monteiro@giantbabybibs.org 71\n", 1653 | "60 Rocio E. Thatch r.ethatch@giantbabybibs.org 75\n", 1654 | "70 M. Hailey Crafton mcrafton@giantbabybibs.org 64\n", 1655 | "150 Latonya F. Latch l.latch@giantbabybibs.org 61" 1656 | ] 1657 | }, 1658 | "metadata": {}, 1659 | "output_type": "display_data" 1660 | }, 1661 | { 1662 | "data": { 1663 | "text/html": [ 1664 | "
\n", 1665 | "\n", 1678 | "\n", 1679 | " \n", 1680 | " \n", 1681 | " \n", 1682 | " \n", 1683 | " \n", 1684 | " \n", 1685 | " \n", 1686 | " \n", 1687 | " \n", 1688 | " \n", 1689 | " \n", 1690 | " \n", 1691 | " \n", 1692 | " \n", 1693 | " \n", 1694 | " \n", 1695 | "
full_nameactual_emailsimilarity
81Sterling Malory Archera.curlingmaloy@giantbabybibs.org56
\n", 1696 | "
" 1697 | ], 1698 | "text/plain": [ 1699 | " full_name actual_email similarity\n", 1700 | "81 Sterling Malory Archer a.curlingmaloy@giantbabybibs.org 56" 1701 | ] 1702 | }, 1703 | "metadata": {}, 1704 | "output_type": "display_data" 1705 | } 1706 | ], 1707 | "source": [ 1708 | "scorer_tester_function('R')\n", 1709 | "display(scorer_test[scorer_test.full_name.str.contains('.', regex=False)])\n", 1710 | "display(scorer_test[81:82])" 1711 | ] 1712 | }, 1713 | { 1714 | "cell_type": "markdown", 1715 | "metadata": {}, 1716 | "source": [ 1717 | "It did! **fuzz.ratio (R)** got **199 out of 200 correct** (99.5% success rate). The only mistake it made was for employee N°81: **Sterling Malory Archer**. (we're in the danger zone!). However, all employees with abbreviated middle names were properly handled with this scorer.\n", 1718 | "\n", 1719 | "Can we do better? Let's try another scorer, **fuzz.partial_ratio (PR)** this time, and see if it's going to work on all problematic employee names :" 1720 | ] 1721 | }, 1722 | { 1723 | "cell_type": "code", 1724 | "execution_count": 22, 1725 | "metadata": {}, 1726 | "outputs": [ 1727 | { 1728 | "data": { 1729 | "text/html": [ 1730 | "
\n", 1731 | "\n", 1744 | "\n", 1745 | " \n", 1746 | " \n", 1747 | " \n", 1748 | " \n", 1749 | " \n", 1750 | " \n", 1751 | " \n", 1752 | " \n", 1753 | " \n", 1754 | " \n", 1755 | " \n", 1756 | " \n", 1757 | " \n", 1758 | " \n", 1759 | " \n", 1760 | " \n", 1761 | " \n", 1762 | " \n", 1763 | " \n", 1764 | " \n", 1765 | " \n", 1766 | " \n", 1767 | " \n", 1768 | " \n", 1769 | " \n", 1770 | " \n", 1771 | " \n", 1772 | " \n", 1773 | " \n", 1774 | " \n", 1775 | " \n", 1776 | " \n", 1777 | " \n", 1778 | " \n", 1779 | "
full_nameactual_emailsimilarity
44Lenora K. Monteirol.monteiro@giantbabybibs.org90
60Rocio E. Thatchr.ethatch@giantbabybibs.org78
70M. Hailey Craftonmcrafton@giantbabybibs.org88
150Latonya F. Latchl.latch@giantbabybibs.org86
\n", 1780 | "
" 1781 | ], 1782 | "text/plain": [ 1783 | " full_name actual_email similarity\n", 1784 | "44 Lenora K. Monteiro l.monteiro@giantbabybibs.org 90\n", 1785 | "60 Rocio E. Thatch r.ethatch@giantbabybibs.org 78\n", 1786 | "70 M. Hailey Crafton mcrafton@giantbabybibs.org 88\n", 1787 | "150 Latonya F. Latch l.latch@giantbabybibs.org 86" 1788 | ] 1789 | }, 1790 | "metadata": {}, 1791 | "output_type": "display_data" 1792 | }, 1793 | { 1794 | "data": { 1795 | "text/html": [ 1796 | "
\n", 1797 | "\n", 1810 | "\n", 1811 | " \n", 1812 | " \n", 1813 | " \n", 1814 | " \n", 1815 | " \n", 1816 | " \n", 1817 | " \n", 1818 | " \n", 1819 | " \n", 1820 | " \n", 1821 | " \n", 1822 | " \n", 1823 | " \n", 1824 | " \n", 1825 | " \n", 1826 | " \n", 1827 | "
full_nameactual_emailsimilarity
81Sterling Malory Archers.archer@giantbabybibs.org88
\n", 1828 | "
" 1829 | ], 1830 | "text/plain": [ 1831 | " full_name actual_email similarity\n", 1832 | "81 Sterling Malory Archer s.archer@giantbabybibs.org 88" 1833 | ] 1834 | }, 1835 | "metadata": {}, 1836 | "output_type": "display_data" 1837 | } 1838 | ], 1839 | "source": [ 1840 | "scorer_tester_function('PR')\n", 1841 | "display(scorer_test[scorer_test.full_name.str.contains('.', regex=False)])\n", 1842 | "display(scorer_test[81:82])" 1843 | ] 1844 | }, 1845 | { 1846 | "cell_type": "markdown", 1847 | "metadata": {}, 1848 | "source": [ 1849 | "Finally! After manually checking all other records, I hereby announce **fuzz.partial_ratio (PR)** the champion with **100% success rate**. The quest is over guys!" 1850 | ] 1851 | }, 1852 | { 1853 | "cell_type": "markdown", 1854 | "metadata": {}, 1855 | "source": [ 1856 | "\n", 1857 | "\n", 1858 | "---\n", 1859 | "[Go back to table of contents ^](#table)\n", 1860 | "# The battle of the scorers :" 1861 | ] 1862 | }, 1863 | { 1864 | "cell_type": "markdown", 1865 | "metadata": {}, 1866 | "source": [ 1867 | "![battle](https://i.ibb.co/Yy4HgmF/battle.jpg)\n", 1868 | "\n", 1869 | "In real world applications, you may be working with huge amounts of data, in which case manually inspecting the results is not an option. That's why you need to understand what each `FuzzyWuzzy` scorer does in order to choose the one that works best for the kind of data you're handling.\n", 1870 | "\n", 1871 | "To explore the differences between the scorers `FuzzyWuzzy` has to offer, we're going to work with a test string. We want to match the **address of a friend** with **multiple addresses from a phonebook** (yes, phonebooks are still a thing!):" 1872 | ] 1873 | }, 1874 | { 1875 | "cell_type": "code", 1876 | "execution_count": 23, 1877 | "metadata": {}, 1878 | "outputs": [], 1879 | "source": [ 1880 | "friend_address = '56 West Princess Rd. Cedar Rapids, IA 52402'\n", 1881 | "\n", 1882 | "phonebook = [ '9102 South St. Lake Worth, FL 33460',\n", 1883 | " '20425 Prince Edward Road, West philadelphia, PA 56560',\n", 1884 | " 'Wst Princess Road, Appt. 56 N°8 3rd floor, C.Rapids, 52402 Iowa ',\n", 1885 | " '400 Van Dyke St.Hartford, IA 26106',\n", 1886 | " '56 Golden Star Rd, Stone Rapids, GA 52483' ]" 1887 | ] 1888 | }, 1889 | { 1890 | "cell_type": "markdown", 1891 | "metadata": {}, 1892 | "source": [ 1893 | "And now let's put all the scorers to the test. We'll use the `fuzz` module instead of `process` in this case." 1894 | ] 1895 | }, 1896 | { 1897 | "cell_type": "markdown", 1898 | "metadata": {}, 1899 | "source": [ 1900 | "* ## fuzz.ratio (R) :
\n", 1901 | "[code explanation 12](#codex12)\n", 1902 | "" 1903 | ] 1904 | }, 1905 | { 1906 | "cell_type": "code", 1907 | "execution_count": 24, 1908 | "metadata": {}, 1909 | "outputs": [ 1910 | { 1911 | "name": "stdout", 1912 | "output_type": "stream", 1913 | "text": [ 1914 | "Friend's address : 56 West Princess Rd. Cedar Rapids, IA 52402 \n", 1915 | " -------------------------------------------------------------- \n", 1916 | "\n", 1917 | "9102 South St. Lake Worth, FL 33460 \n", 1918 | "RATIO: 31 \n", 1919 | "\n", 1920 | "20425 Prince Edward Road, West philadelphia, PA 56560 \n", 1921 | "RATIO: 50 \n", 1922 | "\n", 1923 | "Wst Princess Road, Appt. 56 N°8 3rd floor, C.Rapids, 52402 Iowa \n", 1924 | "RATIO: 62 \n", 1925 | "\n", 1926 | "400 Van Dyke St.Hartford, IA 26106 \n", 1927 | "RATIO: 39 \n", 1928 | "\n", 1929 | "56 Golden Star Rd, Stone Rapids, GA 52483 \n", 1930 | "RATIO: 60 \n", 1931 | "\n" 1932 | ] 1933 | } 1934 | ], 1935 | "source": [ 1936 | "print(\"Friend's address : \", friend_address, '\\n', 62*'-', '\\n')\n", 1937 | "\n", 1938 | "for address in phonebook:\n", 1939 | " ratio = fuzz.ratio(friend_address, address)\n", 1940 | " print(address,'\\nRATIO: ',ratio, '\\n')" 1941 | ] 1942 | }, 1943 | { 1944 | "cell_type": "markdown", 1945 | "metadata": {}, 1946 | "source": [ 1947 | "**fuzz.ratio (R)** simply calculates the **Levenshtein distance**. It only returns 100% if the two strings are exactly similar, uppercase and lowercase included, which makes it useful if we're looking for an exact match." 1948 | ] 1949 | }, 1950 | { 1951 | "cell_type": "markdown", 1952 | "metadata": {}, 1953 | "source": [ 1954 | "* ## fuzz.partial_ratio (PR) :" 1955 | ] 1956 | }, 1957 | { 1958 | "cell_type": "code", 1959 | "execution_count": 25, 1960 | "metadata": {}, 1961 | "outputs": [ 1962 | { 1963 | "name": "stdout", 1964 | "output_type": "stream", 1965 | "text": [ 1966 | "Friend's address : 56 West Princess Rd. Cedar Rapids, IA 52402 \n", 1967 | " -------------------------------------------------------------- \n", 1968 | "\n", 1969 | "9102 South St. Lake Worth, FL 33460 \n", 1970 | "RATIO: 29 \n", 1971 | "\n", 1972 | "20425 Prince Edward Road, West philadelphia, PA 56560 \n", 1973 | "RATIO: 49 \n", 1974 | "\n", 1975 | "Wst Princess Road, Appt. 56 N°8 3rd floor, C.Rapids, 52402 Iowa \n", 1976 | "RATIO: 51 \n", 1977 | "\n", 1978 | "400 Van Dyke St.Hartford, IA 26106 \n", 1979 | "RATIO: 41 \n", 1980 | "\n", 1981 | "56 Golden Star Rd, Stone Rapids, GA 52483 \n", 1982 | "RATIO: 61 \n", 1983 | "\n" 1984 | ] 1985 | } 1986 | ], 1987 | "source": [ 1988 | "print(\"Friend's address : \", friend_address, '\\n', 62*'-', '\\n')\n", 1989 | "\n", 1990 | "for address in phonebook:\n", 1991 | " ratio = fuzz.partial_ratio(friend_address, address)\n", 1992 | " print(address,'\\nRATIO: ',ratio, '\\n')" 1993 | ] 1994 | }, 1995 | { 1996 | "cell_type": "markdown", 1997 | "metadata": {}, 1998 | "source": [ 1999 | "**fuzz.partial_ratio (PR)** takes into account subsets of the strings it compares, and then returns a ratio according to their similarities. For example, it will return a ratio of 100% if it compares **Dwayne The Rock Johnson** with **Dwayne** :" 2000 | ] 2001 | }, 2002 | { 2003 | "cell_type": "code", 2004 | "execution_count": 46, 2005 | "metadata": {}, 2006 | "outputs": [ 2007 | { 2008 | "data": { 2009 | "text/plain": [ 2010 | "100" 2011 | ] 2012 | }, 2013 | "execution_count": 46, 2014 | "metadata": {}, 2015 | "output_type": "execute_result" 2016 | } 2017 | ], 2018 | "source": [ 2019 | "fuzz.partial_ratio('Dwayne The Rock Johnson', 'Dwayne')" 2020 | ] 2021 | }, 2022 | { 2023 | "cell_type": "markdown", 2024 | "metadata": {}, 2025 | "source": [ 2026 | "In our case however, it got fooled because the 5th address has common words with the friend's address like `56`, `Rd.` and `Rapids`. So, perhaps this is not the best scorer to use for this particular task." 2027 | ] 2028 | }, 2029 | { 2030 | "cell_type": "markdown", 2031 | "metadata": {}, 2032 | "source": [ 2033 | "* ## fuzz.token_sort_ratio (TSoR) :" 2034 | ] 2035 | }, 2036 | { 2037 | "cell_type": "code", 2038 | "execution_count": 27, 2039 | "metadata": {}, 2040 | "outputs": [ 2041 | { 2042 | "name": "stdout", 2043 | "output_type": "stream", 2044 | "text": [ 2045 | "Friend's address : 56 West Princess Rd. Cedar Rapids, IA 52402 \n", 2046 | " -------------------------------------------------------------- \n", 2047 | "\n", 2048 | "9102 South St. Lake Worth, FL 33460 \n", 2049 | "RATIO: 35 \n", 2050 | "\n", 2051 | "20425 Prince Edward Road, West philadelphia, PA 56560 \n", 2052 | "RATIO: 65 \n", 2053 | "\n", 2054 | "Wst Princess Road, Appt. 56 N°8 3rd floor, C.Rapids, 52402 Iowa \n", 2055 | "RATIO: 75 \n", 2056 | "\n", 2057 | "400 Van Dyke St.Hartford, IA 26106 \n", 2058 | "RATIO: 38 \n", 2059 | "\n", 2060 | "56 Golden Star Rd, Stone Rapids, GA 52483 \n", 2061 | "RATIO: 57 \n", 2062 | "\n" 2063 | ] 2064 | } 2065 | ], 2066 | "source": [ 2067 | "print(\"Friend's address : \", friend_address, '\\n', 62*'-', '\\n')\n", 2068 | "\n", 2069 | "for address in phonebook:\n", 2070 | " ratio = fuzz.token_sort_ratio(friend_address, address)\n", 2071 | " print(address,'\\nRATIO: ',ratio, '\\n')" 2072 | ] 2073 | }, 2074 | { 2075 | "cell_type": "markdown", 2076 | "metadata": {}, 2077 | "source": [ 2078 | "The Token methods have the advantage of ignoring case and punctuation (all characters get turned to lowercase characters). In the case of **fuzz.token_sort_ratio (TSoR)**, the 'Tokenized' strings (each word is turned into a token) get sorted in alphanumeric order before applying the basic **fuzz.ratio (R)** on them, so the order of the words in both strings compared doesn't matter (unlike the previous non-token methods). In this case, this scorer performed very well." 2079 | ] 2080 | }, 2081 | { 2082 | "cell_type": "markdown", 2083 | "metadata": {}, 2084 | "source": [ 2085 | "* ## fuzz.token_set_ratio (TSeR) :" 2086 | ] 2087 | }, 2088 | { 2089 | "cell_type": "code", 2090 | "execution_count": 28, 2091 | "metadata": {}, 2092 | "outputs": [ 2093 | { 2094 | "name": "stdout", 2095 | "output_type": "stream", 2096 | "text": [ 2097 | "Friend's address : 56 West Princess Rd. Cedar Rapids, IA 52402 \n", 2098 | " -------------------------------------------------------------- \n", 2099 | "\n", 2100 | "9102 South St. Lake Worth, FL 33460 \n", 2101 | "RATIO: 35 \n", 2102 | "\n", 2103 | "20425 Prince Edward Road, West philadelphia, PA 56560 \n", 2104 | "RATIO: 65 \n", 2105 | "\n", 2106 | "Wst Princess Road, Appt. 56 N°8 3rd floor, C.Rapids, 52402 Iowa \n", 2107 | "RATIO: 77 \n", 2108 | "\n", 2109 | "400 Van Dyke St.Hartford, IA 26106 \n", 2110 | "RATIO: 43 \n", 2111 | "\n", 2112 | "56 Golden Star Rd, Stone Rapids, GA 52483 \n", 2113 | "RATIO: 60 \n", 2114 | "\n" 2115 | ] 2116 | } 2117 | ], 2118 | "source": [ 2119 | "print(\"Friend's address : \", friend_address, '\\n', 62*'-', '\\n')\n", 2120 | "\n", 2121 | "for address in phonebook:\n", 2122 | " ratio = fuzz.token_set_ratio(friend_address, address)\n", 2123 | " print(address,'\\nRATIO: ',ratio, '\\n')" 2124 | ] 2125 | }, 2126 | { 2127 | "cell_type": "markdown", 2128 | "metadata": {}, 2129 | "source": [ 2130 | "This is by far the best ratio for the given task. **fuzz.token_set_ratio (TSeR)** is similar to **fuzz.token_sort_ratio (TSoR)**, except it ignores duplicated words (hence the name, because a `set` in Math and also in Python is a collection/data structure that holds no duplicate values). It also conducts a pair to pair comparison on tokens that are common to both strings compared.\n", 2131 | "\n", 2132 | "Example of a set :" 2133 | ] 2134 | }, 2135 | { 2136 | "cell_type": "code", 2137 | "execution_count": 29, 2138 | "metadata": {}, 2139 | "outputs": [ 2140 | { 2141 | "data": { 2142 | "text/plain": [ 2143 | "{'badger', 'mushroom', 'snake'}" 2144 | ] 2145 | }, 2146 | "execution_count": 29, 2147 | "metadata": {}, 2148 | "output_type": "execute_result" 2149 | } 2150 | ], 2151 | "source": [ 2152 | "song = ['badger', 'badger', 'badger', 'mushroom', 'mushroom', 'snake', 'badger']\n", 2153 | "set(song)" 2154 | ] 2155 | }, 2156 | { 2157 | "cell_type": "markdown", 2158 | "metadata": {}, 2159 | "source": [ 2160 | "* ## fuzz.partial_token_sort_ratio (PTSoR) :" 2161 | ] 2162 | }, 2163 | { 2164 | "cell_type": "code", 2165 | "execution_count": 30, 2166 | "metadata": {}, 2167 | "outputs": [ 2168 | { 2169 | "name": "stdout", 2170 | "output_type": "stream", 2171 | "text": [ 2172 | "Friend's address : 56 West Princess Rd. Cedar Rapids, IA 52402 \n", 2173 | " -------------------------------------------------------------- \n", 2174 | "\n", 2175 | "9102 South St. Lake Worth, FL 33460 \n", 2176 | "RATIO: 33 \n", 2177 | "\n", 2178 | "20425 Prince Edward Road, West philadelphia, PA 56560 \n", 2179 | "RATIO: 61 \n", 2180 | "\n", 2181 | "Wst Princess Road, Appt. 56 N°8 3rd floor, C.Rapids, 52402 Iowa \n", 2182 | "RATIO: 71 \n", 2183 | "\n", 2184 | "400 Van Dyke St.Hartford, IA 26106 \n", 2185 | "RATIO: 42 \n", 2186 | "\n", 2187 | "56 Golden Star Rd, Stone Rapids, GA 52483 \n", 2188 | "RATIO: 56 \n", 2189 | "\n" 2190 | ] 2191 | } 2192 | ], 2193 | "source": [ 2194 | "print(\"Friend's address : \", friend_address, '\\n', 62*'-', '\\n')\n", 2195 | "\n", 2196 | "for address in phonebook:\n", 2197 | " ratio = fuzz.partial_token_sort_ratio(friend_address, address)\n", 2198 | " print(address,'\\nRATIO: ',ratio, '\\n')" 2199 | ] 2200 | }, 2201 | { 2202 | "cell_type": "markdown", 2203 | "metadata": {}, 2204 | "source": [ 2205 | "**fuzz.partial_token_sort_ratio (PTSoR)** has the same working as **fuzz.token_sort_ratio (TSR)**, but it uses the **fuzz.partial_ratio (PR)** method after tokenization and sorting rather than the normal **fuzz.ratio (R)** method." 2206 | ] 2207 | }, 2208 | { 2209 | "cell_type": "markdown", 2210 | "metadata": {}, 2211 | "source": [ 2212 | "* ## fuzz.partial_token_set_ratio (PTSeR) :" 2213 | ] 2214 | }, 2215 | { 2216 | "cell_type": "code", 2217 | "execution_count": 31, 2218 | "metadata": {}, 2219 | "outputs": [ 2220 | { 2221 | "name": "stdout", 2222 | "output_type": "stream", 2223 | "text": [ 2224 | "Friend's address : 56 West Princess Rd. Cedar Rapids, IA 52402 \n", 2225 | " -------------------------------------------------------------- \n", 2226 | "\n", 2227 | "9102 South St. Lake Worth, FL 33460 \n", 2228 | "RATIO: 33 \n", 2229 | "\n", 2230 | "20425 Prince Edward Road, West philadelphia, PA 56560 \n", 2231 | "RATIO: 100 \n", 2232 | "\n", 2233 | "Wst Princess Road, Appt. 56 N°8 3rd floor, C.Rapids, 52402 Iowa \n", 2234 | "RATIO: 100 \n", 2235 | "\n", 2236 | "400 Van Dyke St.Hartford, IA 26106 \n", 2237 | "RATIO: 100 \n", 2238 | "\n", 2239 | "56 Golden Star Rd, Stone Rapids, GA 52483 \n", 2240 | "RATIO: 100 \n", 2241 | "\n" 2242 | ] 2243 | } 2244 | ], 2245 | "source": [ 2246 | "print(\"Friend's address : \", friend_address, '\\n', 62*'-', '\\n')\n", 2247 | "\n", 2248 | "for address in phonebook:\n", 2249 | " ratio = fuzz.partial_token_set_ratio(friend_address, address)\n", 2250 | " print(address,'\\nRATIO: ',ratio, '\\n')" 2251 | ] 2252 | }, 2253 | { 2254 | "cell_type": "markdown", 2255 | "metadata": {}, 2256 | "source": [ 2257 | "Same as **fuzz.token_set_ratio (TSR)**, but instead of using **fuzz.ratio (R)** after tokenization and setting (eliminating duplicates), it uses **fuzz.partial_ratio (PR)**. Here we see it is way too forgiving to be useful in this case." 2258 | ] 2259 | }, 2260 | { 2261 | "cell_type": "markdown", 2262 | "metadata": {}, 2263 | "source": [ 2264 | "* ## fuzz.WRatio (WR) :" 2265 | ] 2266 | }, 2267 | { 2268 | "cell_type": "code", 2269 | "execution_count": 32, 2270 | "metadata": { 2271 | "scrolled": true 2272 | }, 2273 | "outputs": [ 2274 | { 2275 | "name": "stdout", 2276 | "output_type": "stream", 2277 | "text": [ 2278 | "Friend's address : 56 West Princess Rd. Cedar Rapids, IA 52402 \n", 2279 | " -------------------------------------------------------------- \n", 2280 | "\n", 2281 | "9102 South St. Lake Worth, FL 33460 \n", 2282 | "RATIO: 38 \n", 2283 | "\n", 2284 | "20425 Prince Edward Road, West philadelphia, PA 56560 \n", 2285 | "RATIO: 62 \n", 2286 | "\n", 2287 | "Wst Princess Road, Appt. 56 N°8 3rd floor, C.Rapids, 52402 Iowa \n", 2288 | "RATIO: 73 \n", 2289 | "\n", 2290 | "400 Van Dyke St.Hartford, IA 26106 \n", 2291 | "RATIO: 44 \n", 2292 | "\n", 2293 | "56 Golden Star Rd, Stone Rapids, GA 52483 \n", 2294 | "RATIO: 64 \n", 2295 | "\n" 2296 | ] 2297 | } 2298 | ], 2299 | "source": [ 2300 | "print(\"Friend's address : \", friend_address, '\\n', 62*'-', '\\n')\n", 2301 | "\n", 2302 | "for address in phonebook:\n", 2303 | " ratio = fuzz.WRatio(friend_address, address)\n", 2304 | " print(address,'\\nRATIO: ',ratio, '\\n')" 2305 | ] 2306 | }, 2307 | { 2308 | "cell_type": "markdown", 2309 | "metadata": {}, 2310 | "source": [ 2311 | "This is the default scorer that gets used with **process.extract()**. This scorer, **fuzz.WRation (WR)** (which stands for Weighted Ratio), is an attempt to get a better ratio than the standard **fuzz.ratio (R)** one. Here is how it works according to the source code:\n", 2312 | "1. Take the ratio of the two processed strings (fuzz.ratio)\n", 2313 | "2. Run checks to compare the length of the strings\n", 2314 | " * If one of the strings is more than 1.5 times as long as the other\n", 2315 | " use partial_ratio comparisons - scale partial results by 0.9\n", 2316 | " (this makes sure only full results can return 100)\n", 2317 | " * If one of the strings is over 8 times as long as the other\n", 2318 | " instead scale by 0.6\n", 2319 | "3. Run the other ratio functions\n", 2320 | " * if using partial ratio functions call partial_ratio,\n", 2321 | " partial_token_sort_ratio and partial_token_set_ratio\n", 2322 | " scale all of these by the ratio based on length\n", 2323 | " * otherwise call token_sort_ratio and token_set_ratio\n", 2324 | " * all token based comparisons are scaled by 0.95\n", 2325 | " (on top of any partial scalars)\n", 2326 | "4. Take the highest value from these results, round it and return it as an integer." 2327 | ] 2328 | }, 2329 | { 2330 | "cell_type": "markdown", 2331 | "metadata": {}, 2332 | "source": [ 2333 | "It's magic I tell you!" 2334 | ] 2335 | }, 2336 | { 2337 | "cell_type": "markdown", 2338 | "metadata": {}, 2339 | "source": [ 2340 | "* ## fuzz.UWRatio (UWR) :" 2341 | ] 2342 | }, 2343 | { 2344 | "cell_type": "code", 2345 | "execution_count": 33, 2346 | "metadata": {}, 2347 | "outputs": [ 2348 | { 2349 | "name": "stdout", 2350 | "output_type": "stream", 2351 | "text": [ 2352 | "Friend's address : 56 West Princess Rd. Cedar Rapids, IA 52402 \n", 2353 | " -------------------------------------------------------------- \n", 2354 | "\n", 2355 | "9102 South St. Lake Worth, FL 33460 \n", 2356 | "RATIO: 38 \n", 2357 | "\n", 2358 | "20425 Prince Edward Road, West philadelphia, PA 56560 \n", 2359 | "RATIO: 62 \n", 2360 | "\n", 2361 | "Wst Princess Road, Appt. 56 N°8 3rd floor, C.Rapids, 52402 Iowa \n", 2362 | "RATIO: 72 \n", 2363 | "\n", 2364 | "400 Van Dyke St.Hartford, IA 26106 \n", 2365 | "RATIO: 44 \n", 2366 | "\n", 2367 | "56 Golden Star Rd, Stone Rapids, GA 52483 \n", 2368 | "RATIO: 64 \n", 2369 | "\n" 2370 | ] 2371 | } 2372 | ], 2373 | "source": [ 2374 | "print(\"Friend's address : \", friend_address, '\\n', 62*'-', '\\n')\n", 2375 | "\n", 2376 | "for address in phonebook:\n", 2377 | " ratio = fuzz.UWRatio(friend_address, address)\n", 2378 | " print(address,'\\nRATIO: ',ratio, '\\n')" 2379 | ] 2380 | }, 2381 | { 2382 | "cell_type": "markdown", 2383 | "metadata": {}, 2384 | "source": [ 2385 | "Same as **WR**, but it takes Unicode into consideration (hence the U). In this case, the 3rd address got a score of 72 instead of 73 that we got with **WR** because of the **° character** that forces the use of Unicode encoding (it not being present in the friend's address made the score slightly lower)." 2386 | ] 2387 | }, 2388 | { 2389 | "cell_type": "markdown", 2390 | "metadata": {}, 2391 | "source": [ 2392 | "* ## fuzz.QRatio (QR) :" 2393 | ] 2394 | }, 2395 | { 2396 | "cell_type": "code", 2397 | "execution_count": 34, 2398 | "metadata": {}, 2399 | "outputs": [ 2400 | { 2401 | "name": "stdout", 2402 | "output_type": "stream", 2403 | "text": [ 2404 | "Friend's address : 56 West Princess Rd. Cedar Rapids, IA 52402 \n", 2405 | " -------------------------------------------------------------- \n", 2406 | "\n", 2407 | "9102 South St. Lake Worth, FL 33460 \n", 2408 | "RATIO: 38 \n", 2409 | "\n", 2410 | "20425 Prince Edward Road, West philadelphia, PA 56560 \n", 2411 | "RATIO: 52 \n", 2412 | "\n", 2413 | "Wst Princess Road, Appt. 56 N°8 3rd floor, C.Rapids, 52402 Iowa \n", 2414 | "RATIO: 63 \n", 2415 | "\n", 2416 | "400 Van Dyke St.Hartford, IA 26106 \n", 2417 | "RATIO: 44 \n", 2418 | "\n", 2419 | "56 Golden Star Rd, Stone Rapids, GA 52483 \n", 2420 | "RATIO: 64 \n", 2421 | "\n" 2422 | ] 2423 | } 2424 | ], 2425 | "source": [ 2426 | "print(\"Friend's address : \", friend_address, '\\n', 62*'-', '\\n')\n", 2427 | "\n", 2428 | "for address in phonebook:\n", 2429 | " ratio = fuzz.QRatio(friend_address, address)\n", 2430 | " print(address,'\\nRATIO: ',ratio, '\\n')" 2431 | ] 2432 | }, 2433 | { 2434 | "cell_type": "markdown", 2435 | "metadata": {}, 2436 | "source": [ 2437 | "According to the source code, **QR** stands for Quick Ratio because it short circuits the comparison process if either of the strings is empty after processing." 2438 | ] 2439 | }, 2440 | { 2441 | "cell_type": "markdown", 2442 | "metadata": {}, 2443 | "source": [ 2444 | "* ## fuzz.UQRatio (UQR) :" 2445 | ] 2446 | }, 2447 | { 2448 | "cell_type": "code", 2449 | "execution_count": 36, 2450 | "metadata": {}, 2451 | "outputs": [ 2452 | { 2453 | "name": "stdout", 2454 | "output_type": "stream", 2455 | "text": [ 2456 | "Friend's address : 56 West Princess Rd. Cedar Rapids, IA 52402 \n", 2457 | " -------------------------------------------------------------- \n", 2458 | "\n", 2459 | "9102 South St. Lake Worth, FL 33460 \n", 2460 | "RATIO: 38 \n", 2461 | "\n", 2462 | "20425 Prince Edward Road, West philadelphia, PA 56560 \n", 2463 | "RATIO: 52 \n", 2464 | "\n", 2465 | "Wst Princess Road, Appt. 56 N°8 3rd floor, C.Rapids, 52402 Iowa \n", 2466 | "RATIO: 62 \n", 2467 | "\n", 2468 | "400 Van Dyke St.Hartford, IA 26106 \n", 2469 | "RATIO: 44 \n", 2470 | "\n", 2471 | "56 Golden Star Rd, Stone Rapids, GA 52483 \n", 2472 | "RATIO: 64 \n", 2473 | "\n" 2474 | ] 2475 | } 2476 | ], 2477 | "source": [ 2478 | "print(\"Friend's address : \", friend_address, '\\n', 62*'-', '\\n')\n", 2479 | "\n", 2480 | "for address in phonebook:\n", 2481 | " ratio = fuzz.UQRatio(friend_address, address)\n", 2482 | " print(address,'\\nRATIO: ',ratio, '\\n')" 2483 | ] 2484 | }, 2485 | { 2486 | "cell_type": "markdown", 2487 | "metadata": {}, 2488 | "source": [ 2489 | "Same as **QR** but it stands for Unicode Quick Ratio (just like there is a Unicode version of **WR**)" 2490 | ] 2491 | }, 2492 | { 2493 | "cell_type": "markdown", 2494 | "metadata": {}, 2495 | "source": [ 2496 | "\n", 2497 | "\n", 2498 | "---\n", 2499 | "[Go back to table of contents ^](#table)\n", 2500 | "# The verdict :" 2501 | ] 2502 | }, 2503 | { 2504 | "cell_type": "markdown", 2505 | "metadata": {}, 2506 | "source": [ 2507 | "As we can see, the best performing scorer changes from an application to an other. In the case of comparing full names to emails, **fuzz.partial_ratio (PR)** is the way to go. For addresses, **fuzz.token_set_ratio (TSeR)** is the scorer that performed best, not only because it gave the highest ratio to the correct address, but because the difference between the ratios of the correct address and the closest wrong address is the highest (12 points).\n", 2508 | "\n", 2509 | "In any case, using `FuzzyWuzzy` needs some experimenting before settling for the best method to match strings." 2510 | ] 2511 | }, 2512 | { 2513 | "cell_type": "markdown", 2514 | "metadata": {}, 2515 | "source": [ 2516 | "\n", 2517 | "\n", 2518 | "---\n", 2519 | "[Go back to table of contents ^](#table)\n", 2520 | "# The appendix (code explanations) :" 2521 | ] 2522 | }, 2523 | { 2524 | "cell_type": "markdown", 2525 | "metadata": {}, 2526 | "source": [ 2527 | "\n", 2528 | "* ### Code explanation 1: \n", 2529 | "[Go back to the code](#code1)" 2530 | ] 2531 | }, 2532 | { 2533 | "cell_type": "markdown", 2534 | "metadata": {}, 2535 | "source": [ 2536 | "```\n", 2537 | "%%HTML\n", 2538 | "```" 2540 | ] 2541 | }, 2542 | { 2543 | "cell_type": "markdown", 2544 | "metadata": {}, 2545 | "source": [ 2546 | "This cell uses the magic function `%%HTML` to render the CSS style between the `style` HTML tags. Here is what the CSS does :\n", 2547 | "\n", 2548 | "
\n",
2549 |     ".dataframe th, td:first-child {                This calls on the selectors for dataframe index cells\n",
2550 |     "background:      #3f577c;                      This changes the background color of dataframe index cells\n",
2551 |     "font-family:     monospace;                    This changes their font to monospace\n",
2552 |     "color:           white;                        This colors the text white\n",
2553 |     "border:          3px solid white;              This adds a 3 pixel white border around dataframe index cells\n",
2554 |     "text-align:      left !important;              This aligns the dataframe index cells text to the left\n",
2555 |     "} \n",
2556 |     "\n",
2557 |     "#codex {                                       This calls on the ID for the 'code explanation' links\n",
2558 |     "float:           right;                        This sends the 'code explanation' links to the right\n",
2559 |     "}\n",
2560 |     "
" 2561 | ] 2562 | }, 2563 | { 2564 | "cell_type": "markdown", 2565 | "metadata": {}, 2566 | "source": [ 2567 | "\n", 2568 | "* ### Code explanation 2: \n", 2569 | "[Go back to the code](#code2)" 2570 | ] 2571 | }, 2572 | { 2573 | "cell_type": "markdown", 2574 | "metadata": {}, 2575 | "source": [ 2576 | "```\n", 2577 | "hr['gen_email'] = hr['full_name'].str[0] + '.' + hr['full_name'].str.split().str[1] + '@giantbabybibs.org'\n", 2578 | "hr['gen_email'] = hr['gen_email'].str.lower()\n", 2579 | "hr.head(3)```" 2580 | ] 2581 | }, 2582 | { 2583 | "cell_type": "markdown", 2584 | "metadata": {}, 2585 | "source": [ 2586 | "In the first line, we are creating a new column `hr['gen_email]` that takes the value of 4 parts concatenated (joined) together with the `+` sign. The 4 parts are:\n", 2587 | "* `hr['full_name'].str[0]`: takes the first letter of the full name\n", 2588 | "* `'.'`: dot\n", 2589 | "* `hr['full_name'].str.split().str[1]`: takes the full name, splits it into words (based on the white space) and then turns the 2nd word (which is the last name for employees with no middle name). Indexing in python is zero based, so an index of 1 returns the 2nd element.\n", 2590 | "* `'@giantbabybibs.org'`: adds the domain name\n", 2591 | "\n", 2592 | "The second line simply turns all characters within the new column into lowercase characters." 2593 | ] 2594 | }, 2595 | { 2596 | "cell_type": "markdown", 2597 | "metadata": {}, 2598 | "source": [ 2599 | "\n", 2600 | "* ### Code explanation 3: \n", 2601 | "[Go back to the code](#code3)" 2602 | ] 2603 | }, 2604 | { 2605 | "cell_type": "markdown", 2606 | "metadata": {}, 2607 | "source": [ 2608 | "```\n", 2609 | "hr['exists'] = hr.apply(lambda x: x.gen_email in list(it.email), axis=1)\n", 2610 | "hr.head(3)```" 2611 | ] 2612 | }, 2613 | { 2614 | "cell_type": "markdown", 2615 | "metadata": {}, 2616 | "source": [ 2617 | "This line creates a new column with `hr['exists']` and then applies a function `lambda x: x.gen_email in list(it.email)` that takes every email in the `gen_email` column and checks if it exists in the actual emails list `it.email`. It returns `True` if it exists, and `False` if not.\n", 2618 | "\n", 2619 | "The `axis` parameter here indicates that we want to apply the function along the column (0=index, 1=column)." 2620 | ] 2621 | }, 2622 | { 2623 | "cell_type": "markdown", 2624 | "metadata": {}, 2625 | "source": [ 2626 | "\n", 2627 | "* ### Code explanation 4: \n", 2628 | "[Go back to the code](#code4)" 2629 | ] 2630 | }, 2631 | { 2632 | "cell_type": "markdown", 2633 | "metadata": {}, 2634 | "source": [ 2635 | "The code is condensed only for it to take as little space as possible. Normally, for the sake of clarity, it should be written this way :\n", 2636 | "\n", 2637 | "```\n", 2638 | "ax = hr.exists.value_counts().sort_values().plot( kind='barh', # Horizontal bar plot\n", 2639 | " color=['tomato', 'c'], # The colors of the two bars\n", 2640 | " figsize=(12,1), # The size of the plot\n", 2641 | " width=0.3 ) # The width of the bars\n", 2642 | " \n", 2643 | "plt.xlim(0,201) # Limit the x axis to the range of 0-200 (we added 1 so 200 would be visible)\n", 2644 | "plt.xticks(fontsize=8) # Font size of 8 for the x axis ticks\n", 2645 | "ax.set_frame_on(False) # Remove the frame around the plot for simplicity\n", 2646 | "plt.grid( color='white', # Add a grid with white lines\n", 2647 | " alpha=.4, # and an opacity of 40%\n", 2648 | " axis='x' ) ; # only for the x axis (vertically) ``` " 2649 | ] 2650 | }, 2651 | { 2652 | "cell_type": "markdown", 2653 | "metadata": {}, 2654 | "source": [ 2655 | "Here, we take `hr.exists.value_counts()`, that outputs the following :\n", 2656 | " \n", 2657 | " True 148\n", 2658 | " False 52\n", 2659 | " \n", 2660 | "And then we use `sort_values()` on it in order to make the `True` bar come first in the plot. Then, we use matplotlib to plot a horizontal bar plot with `.plot( kind='barh', ...)`\n", 2661 | "\n", 2662 | "The semi-colon ';' at the very end prevents the output from the last code line to show up. Here's why :\n", 2663 | "\n", 2664 | "**Plot without semi-colon :**" 2665 | ] 2666 | }, 2667 | { 2668 | "cell_type": "code", 2669 | "execution_count": 41, 2670 | "metadata": {}, 2671 | "outputs": [ 2672 | { 2673 | "data": { 2674 | "text/plain": [ 2675 | "((0, 201),\n", 2676 | " (array([ 0., 25., 50., 75., 100., 125., 150., 175., 200., 225.]),\n", 2677 | " ),\n", 2678 | " None,\n", 2679 | " None)" 2680 | ] 2681 | }, 2682 | "execution_count": 41, 2683 | "metadata": {}, 2684 | "output_type": "execute_result" 2685 | }, 2686 | { 2687 | "data": { 2688 | "image/png": "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\n", 2689 | "text/plain": [ 2690 | "
" 2691 | ] 2692 | }, 2693 | "metadata": { 2694 | "needs_background": "light" 2695 | }, 2696 | "output_type": "display_data" 2697 | } 2698 | ], 2699 | "source": [ 2700 | "ax = hr.exists.value_counts().sort_values().plot(kind='barh', color=['tomato', 'c'], figsize=(12,1), width=0.3)\n", 2701 | "plt.xlim(0,201), plt.xticks(fontsize=8), ax.set_frame_on(False), plt.grid(color='white', alpha=.4, axis='x')" 2702 | ] 2703 | }, 2704 | { 2705 | "cell_type": "markdown", 2706 | "metadata": {}, 2707 | "source": [ 2708 | "**Plot with semi-colon :**" 2709 | ] 2710 | }, 2711 | { 2712 | "cell_type": "code", 2713 | "execution_count": 39, 2714 | "metadata": {}, 2715 | "outputs": [ 2716 | { 2717 | "data": { 2718 | "image/png": "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\n", 2719 | "text/plain": [ 2720 | "
" 2721 | ] 2722 | }, 2723 | "metadata": { 2724 | "needs_background": "light" 2725 | }, 2726 | "output_type": "display_data" 2727 | } 2728 | ], 2729 | "source": [ 2730 | "ax = hr.exists.value_counts().sort_values().plot(kind='barh', color=['tomato', 'c'], figsize=(12,1), width=0.3)\n", 2731 | "plt.xlim(0,201), plt.xticks(fontsize=8), ax.set_frame_on(False), plt.grid(color='white', alpha=.4, axis='x') ;" 2732 | ] 2733 | }, 2734 | { 2735 | "cell_type": "markdown", 2736 | "metadata": {}, 2737 | "source": [ 2738 | "\n", 2739 | "* ### Code explanation 5: \n", 2740 | "[Go back to the code](#code5)" 2741 | ] 2742 | }, 2743 | { 2744 | "cell_type": "markdown", 2745 | "metadata": {}, 2746 | "source": [ 2747 | "```\n", 2748 | "it['username'] = it['email'].str.replace('@giantbabybibs.org', '')\n", 2749 | "it.head(3)```" 2750 | ] 2751 | }, 2752 | { 2753 | "cell_type": "markdown", 2754 | "metadata": {}, 2755 | "source": [ 2756 | "This is a simple one: we use the Pandas attribute (or accessor) `str`, followed by the method `.replace()` in order to replace the domaine name with an empty character (which has the same result as deleting the domaine name). The values are then assigned to the newly created column `username`." 2757 | ] 2758 | }, 2759 | { 2760 | "cell_type": "markdown", 2761 | "metadata": {}, 2762 | "source": [ 2763 | "\n", 2764 | "* ### Code explanation 6: \n", 2765 | "[Go back to the code](#code6)" 2766 | ] 2767 | }, 2768 | { 2769 | "cell_type": "markdown", 2770 | "metadata": {}, 2771 | "source": [ 2772 | "```\n", 2773 | "actual_email = []\n", 2774 | "similarity = []\n", 2775 | "\n", 2776 | "for i in hr.full_name:\n", 2777 | " ratio = process.extract( i, it.username, limit=1)\n", 2778 | " actual_email.append(ratio[0][0])\n", 2779 | " similarity.append(ratio[0][1])\n", 2780 | "\n", 2781 | "hr['actual_email'] = pd.Series(actual_email)\n", 2782 | "hr['actual_email'] = hr['actual_email'] + '@giantbabybibs.org'\n", 2783 | "hr['similarity'] = pd.Series(similarity)\n", 2784 | "\n", 2785 | "hr.head(3)\n", 2786 | "```" 2787 | ] 2788 | }, 2789 | { 2790 | "cell_type": "markdown", 2791 | "metadata": {}, 2792 | "source": [ 2793 | "There are 3 steps here :\n", 2794 | "1. We create two empty lists: `actual_email` and `similarity` ;\n", 2795 | "2. We use a `for` loop that iterates over each element of `hr.full_name` and applies the indented code or statement after it. Let's see what that statement does :\n", 2796 | "\n", 2797 | " * `ratio = process.extract( i, it.username, limit=1)`: this calculates the ratio of `i` (which is a full name from `r.full_name` compared to all elements in `it.username`. Normally, this would return a list of tuples containing all the elements in `it.username` coupled with their respective ratios and indexes. However, because we specified the parameter `limit=1`, we'll only get the highest rated element. If we run this bit of code alone, we'll get :\n", 2798 | " \n", 2799 | "
process.extract( 'Tony Stark', it.username, limit=1)\n",
2800 |     "         Output: [('t.stark', 82, 182)]
\n", 2801 | " \n", 2802 | " As you can see, we get the matching element `t.stark`, its ratio `82` and its index in `it.username` which is `182`. Now this whole list is assigned to a variable called `ratio`.\n", 2803 | "\n", 2804 | " * `actual_email.append(ratio[0][0])`: in this line, we take the element ratio[0][0], which is the first element in the first tuple `t.stark`, and we append it (add it) to the list `actual_email`.\n", 2805 | " * `similarity.append(ratio[0][1])`: likewise, we take the element ratio[0][1], which is the second element in the first tuple `82` and we append it to the list `similarity`.\n", 2806 | " \n", 2807 | " By the end of this loop, we'll end up with two lists: `actual_email` filled with all the best matching usernames, and `similarity` that holds all of their respective ratios.\n", 2808 | " \n", 2809 | " \n", 2810 | "3. The third part comes after the completion of the for loop. Here, we create a new column called `actual_email` and we populate if with all the values from the list `actual_email`, and then we add the domaine name to it to make an email out of the usernames. We do the same thing with `similarity`." 2811 | ] 2812 | }, 2813 | { 2814 | "cell_type": "markdown", 2815 | "metadata": {}, 2816 | "source": [ 2817 | "\n", 2818 | "* ### Code explanation 7: \n", 2819 | "[Go back to the code](#code7)" 2820 | ] 2821 | }, 2822 | { 2823 | "cell_type": "markdown", 2824 | "metadata": {}, 2825 | "source": [ 2826 | "```\n", 2827 | "final_result = hr[['full_name', 'actual_email', 'similarity']]\n", 2828 | "final_result.head(3)\n", 2829 | "```" 2830 | ] 2831 | }, 2832 | { 2833 | "cell_type": "markdown", 2834 | "metadata": {}, 2835 | "source": [ 2836 | "In Pandas, if you have a dataframe with many columns, you can choose which columns to show by using double brackets [[ ]]. You can also make a new dataframe out of this selection if you assign it to a variable. However, make sure changes to the selection do not impact the original dataframe (in our case, we don't have to worry about that because no changes were made to the newly created `final_resul` dataframe)." 2837 | ] 2838 | }, 2839 | { 2840 | "cell_type": "markdown", 2841 | "metadata": {}, 2842 | "source": [ 2843 | "\n", 2844 | "* ### Code explanation 8: \n", 2845 | "[Go back to the code](#code8)" 2846 | ] 2847 | }, 2848 | { 2849 | "cell_type": "markdown", 2850 | "metadata": {}, 2851 | "source": [ 2852 | "```\n", 2853 | "final_result.head(8).style.background_gradient(subset='similarity', cmap='summer_r') \\\n", 2854 | " .set_table_styles(styles)\n", 2855 | "```" 2856 | ] 2857 | }, 2858 | { 2859 | "cell_type": "markdown", 2860 | "metadata": {}, 2861 | "source": [ 2862 | "In this code, we use `style.background_gradient` to apply the colormap `summer_r` to the subset or column `similarity`. `set_table_styles` reapplies the CSS we first introduced in at the beginning. The backslash or reverse slash is used here to mark a new line (or line break) without breaking the code (very useful if you want to keep clarity with long lines of code)." 2863 | ] 2864 | }, 2865 | { 2866 | "cell_type": "markdown", 2867 | "metadata": {}, 2868 | "source": [ 2869 | "\n", 2870 | "* ### Code explanation 9: \n", 2871 | "[Go back to the code](#code9)" 2872 | ] 2873 | }, 2874 | { 2875 | "cell_type": "markdown", 2876 | "metadata": {}, 2877 | "source": [ 2878 | "```\n", 2879 | "final_result[final_result['full_name'].str.contains('.', regex=False)]\n", 2880 | "```" 2881 | ] 2882 | }, 2883 | { 2884 | "cell_type": "markdown", 2885 | "metadata": {}, 2886 | "source": [ 2887 | "We use the `str` accessor and its method `contains` to filter all rows in which the full name contains the character DOT. We're turning off Regex here because DOT is a literal string we're looking for and not a Regular Expression (RegEx)." 2888 | ] 2889 | }, 2890 | { 2891 | "cell_type": "markdown", 2892 | "metadata": {}, 2893 | "source": [ 2894 | "\n", 2895 | "* ### Code explanation 10: \n", 2896 | "[Go back to the code](#code10)" 2897 | ] 2898 | }, 2899 | { 2900 | "cell_type": "markdown", 2901 | "metadata": {}, 2902 | "source": [ 2903 | "```\n", 2904 | "def scorer_tester_function(x) :\n", 2905 | " actual_email = []\n", 2906 | " similarity = []\n", 2907 | " \n", 2908 | " for i in scorer_test['full_name']:\n", 2909 | " ratio = process.extract( i, it.username, limit=1, scorer=scorer_dict[x])\n", 2910 | " actual_email.append( ratio[0][0] )\n", 2911 | " similarity.append( ratio[0][1] )\n", 2912 | "\n", 2913 | " scorer_test['actual_email'] = pd.Series(actual_email)\n", 2914 | " scorer_test['actual_email'] = scorer_test['actual_email'] + '@giantbabybibs.org'\n", 2915 | " scorer_test['similarity'] = pd.Series(similarity)\n", 2916 | " \n", 2917 | " return scorer_test\n", 2918 | "```" 2919 | ] 2920 | }, 2921 | { 2922 | "cell_type": "markdown", 2923 | "metadata": {}, 2924 | "source": [ 2925 | "Here, we take the code in Code expalantion 7 and slap it inside a function (so we don't have to retype the whole code every time we want to execute it). The function takes one parameter, which is the scorer, and returns a dataframe `scorer_test`. And because we specified here `scorer = scorer_dict[x]`, we only need to supply the abbrevation of a scorer as the x paramter for the function to work. " 2926 | ] 2927 | }, 2928 | { 2929 | "cell_type": "markdown", 2930 | "metadata": {}, 2931 | "source": [ 2932 | "\n", 2933 | "* ### Code explanation 11: \n", 2934 | "[Go back to the code](#code11)" 2935 | ] 2936 | }, 2937 | { 2938 | "cell_type": "markdown", 2939 | "metadata": {}, 2940 | "source": [ 2941 | "```\n", 2942 | "scorer_tester_function('WR')\n", 2943 | "scorer_test[scorer_test.full_name.str.contains('.', regex=False)]\n", 2944 | "```" 2945 | ] 2946 | }, 2947 | { 2948 | "cell_type": "markdown", 2949 | "metadata": {}, 2950 | "source": [ 2951 | "See Code explanation 10 and then Code explanation 9." 2952 | ] 2953 | }, 2954 | { 2955 | "cell_type": "markdown", 2956 | "metadata": {}, 2957 | "source": [ 2958 | "\n", 2959 | "* ### Code explanation 12: \n", 2960 | "[Go back to the code](#code12)" 2961 | ] 2962 | }, 2963 | { 2964 | "cell_type": "markdown", 2965 | "metadata": {}, 2966 | "source": [ 2967 | "```\n", 2968 | "print(\"Friend's address : \", friend_address, '\\n', 62*'-', '\\n')\n", 2969 | "\n", 2970 | "for address in phonebook:\n", 2971 | " ratio = fuzz.ratio(friend_address, address)\n", 2972 | " print(address,'\\nRATIO: ',ratio, '\\n')\n", 2973 | "```" 2974 | ] 2975 | }, 2976 | { 2977 | "cell_type": "markdown", 2978 | "metadata": {}, 2979 | "source": [ 2980 | "This is a simple one. Inside the parenthese of the `print` function, we have elements we want to print (show on the screen), separated by commas. The first element, `\"Friend's address : \"` is a simple string. The second is the variable `friend_address`. The third is a new line or a line break. The fourth is dash printed 62 times. The fifth is another line break. You can use the `+` operator to link all of these elements inside `print()`, but then you'll have to convert them all into string for it to work:\n", 2981 | "\n", 2982 | "`print( \"Friend's address : \" + str(friend_address) + '\\n', 62*'-', '\\n')`\n", 2983 | "\n", 2984 | "The second part is simply a `for` loop that goes over all the addresses in the phonebook and calculates their ratios in comparison to the friend's address." 2985 | ] 2986 | }, 2987 | { 2988 | "cell_type": "markdown", 2989 | "metadata": {}, 2990 | "source": [ 2991 | "\n", 2992 | "\n", 2993 | "---\n", 2994 | "[Go back to table of contents ^](#table)\n", 2995 | "# Parting notes :" 2996 | ] 2997 | }, 2998 | { 2999 | "cell_type": "markdown", 3000 | "metadata": {}, 3001 | "source": [ 3002 | "I just hope I didn't bore you guys to death!" 3003 | ] 3004 | } 3005 | ], 3006 | "metadata": { 3007 | "kernelspec": { 3008 | "display_name": "Python 3", 3009 | "language": "python", 3010 | "name": "python3" 3011 | }, 3012 | "language_info": { 3013 | "codemirror_mode": { 3014 | "name": "ipython", 3015 | "version": 3 3016 | }, 3017 | "file_extension": ".py", 3018 | "mimetype": "text/x-python", 3019 | "name": "python", 3020 | "nbconvert_exporter": "python", 3021 | "pygments_lexer": "ipython3", 3022 | "version": "3.6.7" 3023 | } 3024 | }, 3025 | "nbformat": 4, 3026 | "nbformat_minor": 2 3027 | } 3028 | --------------------------------------------------------------------------------