Skip to content
Advertisement

Get index of row where pandas column contains regex

I am checking to see if a pandas column matches a pre-defined regex, using .any() to get the first match if found. However, I need to return the index/row where this match occurred so that I can get the value of another column in that row.

I have the below to check where the reg_ex pattern exists in df['id_org']

JavaScript

Once the above evaluates to true, how do I get the index/row that caused the expression to evaluate to true? I would like to use this index so that I can access another column for that same row using pandas df.at[index, 'desired_col'] or .iloc functions.

In the past I have done: df.at[df['id_org'][df['id_org'] == key].index[0], 'desired_col'] however, I can’t use this line of code any more because I am no longer checking for an exact string “key” match bur rather when a regex now matches in that column.

Advertisement

Answer

You can use idxmax combined with any:

JavaScript

Or first_valid_index:

JavaScript

Example of outputs:

JavaScript

Used input:

JavaScript

all matches

JavaScript

Example for the regex 'e': Int64Index([1, 3], dtype='int64')

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