0

Apologise if I don't make sense in advance as I'm still working out 100% of the terminology, but am pretty good at Excel and am on my Python/Numpy journey.

Currently working with CSVs out of what I would compare to an IT Ticket system which has various columns which are fairly consistent to groupby, that part I'm ok with.

One column, in particular, is free-text to explain the issue but users may include a Error code; in this example, we will say its format is always in the format of "ERR#####" Aka ERR54321. "ERR" being the constant and always followed by 5 numerals.

Is there a best method / way to somehow extract that particular value and then create it into its own column in the dataframe for that row?

Goal is to be able to do this so I can quantify the volume/frequency of the errors being provided.

Thanks in Advance!

2
  • Would have been a complete question had you included an example of your data like the posted answer. :-) Commented May 30, 2020 at 15:49
  • Thanks Anky, I'll certainly think of that next time rather than explaining it haha. Commented Jun 1, 2020 at 11:10

1 Answer 1

1

You can use the power of regular expression on the dataframe:

import pandas as pd

# prepare demo df
data = ["got ERR12345 today", "ERR 0815", "to ERR or not to ERR", "no ERR11111 now"]
df = pd.DataFrame({"code" : data}) 

# use regex to extract stuff and create a new column
df["ERR"] = df["code"].str.extract(r"(ERR\d{5})")

print(df) 

and create a new column by it:

                   code       ERR
0    got ERR12345 today  ERR12345
1              ERR 0815       NaN
2  to ERR or not to ERR       NaN
3       no ERR11111 now  ERR11111

Related links:

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Patrick for this; It's working perfectly and have been able to use it in different methods as well. its definitely made more sense and I appreciate the related links, will certainly give them a read!

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.