1
dframe = pd.DataFrame({'Num':['38-30','38-30','21-51','24-11','34-20'],
'Des':['Generates Vacuum','Pressure low','Ground Problem',
'Leak from Controller','Lock Unserviceable']})

In the above dataframe I want to extract specific strings from Des column based on the Num column. For instance, if Num is equal to 38-30 then extract Vacuum from Des and if it doesn't have Vacuum, then extract Pressure. There are multiple strings that I have to extract for each Num.

I am trying to use re.extract('generator|Pressure',re.I) but I don't know how to include the if statements as I mentioned above.

My output should look like this: enter image description here

5
  • 1
    kindly post ur expected output Commented May 22, 2020 at 1:18
  • I've aded the expected output. Thank you! Commented May 22, 2020 at 18:30
  • Seems u hav multiple conditions as evidenced from ur output Commented May 22, 2020 at 23:06
  • Yes. Is there a way that I can achieve that with Regex and If conditions? Commented May 27, 2020 at 21:44
  • @YOBEN's answer below involves regex and a vectorized IF statement Commented May 27, 2020 at 21:45

1 Answer 1

1

Use np.where with findall

np.where(df.Num.eq('38-30'),df.Des.str.findall('generator|vacuum',flags=re.IGNORECASE),'')
Sign up to request clarification or add additional context in comments.

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.