3

Here is my problem.

I know how to create a boolean column based on RegEx, like this:

df['New Column'] = df.columnA.str.match(regex)

In this example, 'New Column' will be contain True or False values.

But how do I do if I want to use a condition to say "If my RegEx returns true, push "this" value, and if it returns False, then push "that" value.

Thank you for your help :)

0

3 Answers 3

7

You can use the where() function from NumPy:

df['New Column'] = np.where(df.columnA.str.match(regex), "this", "that")

You can use other column names instead of the scalars:

df['New Column'] = np.where(df.columnA.str.match(regex), df.columnB, df.columnC)
Sign up to request clarification or add additional context in comments.

2 Comments

Hello again @DYZ. How can I do if want to process multiple possibilities and not just a binary condition? How can I use my own function, like for instance "return myfunction(df.columnA)" ? Thank you for your help!
Ok I just made a test and it's easy, I just need to call my function with the column in argument and it works. Pyhon is definitely magic.
2

Since you have already got the series of booleans, why not a simple map?

df['New Column'] = list(map(lambda b : 'this' if b else 'that', df.foo.str.match('foo.')))

Comments

-1

Edit:

df['New Column'] = ["this" if row.str.match(regex) else "that" for row in df.columnA]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.