Skip to content Skip to sidebar Skip to footer

Search Excel Columns For Matching Text Value, Print Row #s

Here I grab the name and zip values from a different document; and store in variables: (works fine) Name = find_name.group(0) Then I simply want to search my excel fil

Solution 1:

I do not have your excel file, so I setup the following code:

import pandas as pd
names = ["RHONDA GILBERT", "FRED FLINTSTONE", "FRED FLINTSTONE", "BARNEY RUBLE", "RHONDA GILBERT"]
add1 = ["123 Elm St", "254 Pine Ave", "254 Pine Ave", "654 Spruce Grove", "123 Elm St"]
df = pd.DataFrame(list(zip(names, add1)), 
   columns =['Member Name', 'Member Address Line 1']) 
df

It gives me the following output:

Member Name     Member Address Line 10   RHONDA GILBERT  123 Elm St
1   FRED FLINTSTONE 254 Pine Ave
2   FRED FLINTSTONE 254 Pine Ave
3   BARNEY RUBLE    654 Spruce Grove
4   RHONDA GILBERT  123 Elm St

If I now search for "FRED" then I write it like so:

Name = "FRED"
matches = df[df['Member Name'].str.contains(Name)]
matches

and the output I get is this:

Member Name     Member  Address Line 11   FRED FLINTSTONE 254 Pine Ave
2   FRED FLINTSTONE 254 Pine Ave

Note that if I ask for the indices of matches I get

matches.index
# outputs
Int64Index([1, 2], dtype='int64')

These are the original indices of df. So then looking for the minimum value of the index

matches.index.min()
# outputs1

This is the minimum of the indices. I am not too sure how your results deviated from the above. If you care to clarify, I will alter my explanation.

Post a Comment for "Search Excel Columns For Matching Text Value, Print Row #s"