Skip to content
Advertisement

Python Pandas find rows that match a pattern using column first characters and a set of values to match

I have a sorted DataFrame by company_name:

JavaScript

I would like to select the rows which have the first 3 letters in common and have the following rows ending with “u” or “w”.

Ideally I would like the result to look like this (including the “main” name as an extra column).

JavaScript

Assume that the start of the company_name has to contain u or w, the end of the name can differ. Best

Advertisement

Answer

Let’s try:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement