0

I have a csv file that has the block of text in a field that I would like to extract to a new column. For example my csv looks like this below:

house, paint, status-text house1, green, this house is nice it gets a status of result: PASS this is good house2, red, this house is not too nice it gets a status of result: FAIL this is bad house3, blue, this house is the best it gets a status of result: PASS this is great,

I would like to run a simple regex to pull out the (result: PASS) or (result: FAIL) into a new column so the CSV would now look like below:

house, paint, status-text, status house1, green, this house is nice it gets a status of result: PASS this is good, PASS house2, red, this house is not too nice it gets a status of result: FAIL this is bad, FAIL house3, blue, this house is the best it gets a status of result: PASS this is great, PASS

I was thinking of using a Pandas data frame but not sure how to parse out the (PASS/FAIL) and move it to its own column for the 3 rows, and potentially having this scale to hundreds of rows. Any example of how to do this as a small sample would be greatly appreciated.

2 Answers 2

1

you can load the csv to pandas dataframe and then do this

    conditions = [
        df["status-text"].str.contains("result: PASS "),
        df["status-text"].str.contains("result: FAIL "),
    ]

    choices = ["PASS", "FAIL"]

    df["status"] = numpy.select(conditions, choices, default=None)

    print(df)
Sign up to request clarification or add additional context in comments.

1 Comment

Wow this was a great, easy to understand and elegant solution. I was able to integrate it into my poc. Thanks so much!
1

You can use np.where

df['status'] = np.where(df["status-text"].str.contains('PASS'), 'PASS', 'FAIL')
df
    house   paint                                        status-text status
0  house1   green   this house is nice it gets a status of result...   PASS
1  house2     red   this house is not too nice it gets a status o...   FAIL
2  house3    blue   this house is the best it gets a status of re...   PASS

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.