david li david li - 2 months ago 16
Python Question

Search through column for multiple matching integers

I'm trying to search through a database with a column that can have one or more integer values, to match one or more given integers. I just started using Python and Pandas.

The integers in the database has a '-' in between For example

--------------------------------------------------
| Customer 1 | 1143 |
--------------------------------------------------
| Customer 2 |1124-1123|
--------------------------------------------------
| Customer 3 |1254-1234-1642 |
--------------------------------------------------


I can't quite figure out how to parse the values, and at the same time make it so that order doesn't matter too. So let's say I have 1123 and 1124 as the two integers to find a match to, end goal here is to display the customer with the matching integers.

Any input would be greatly appreciated!

Answer Source

Based on your clarification, I believe a full regex match is necessary. You can use df.str.contains with a regex pattern:

In [705]: numbers = ['1124', '1123']

In [709]: df
Out[709]: 
     Customer          Number
0  Customer 1            1143
1  Customer 2       1124-1123
2  Customer 3  1254-1234-1642
3  Customer 4  1124-1123-1125

In [710]: df[df.Number.str.contains('^' + '-'.join(numbers) + '$', regex=True)]
Out[710]: 
     Customer     Number
1  Customer 2  1124-1123