Python code to search incident number
Sample Code to Extract Incident Numbers:
import pandas as pd
import re
# Sample data
data = {
'RTSK Worknote': [
'Some text INC1032699533 more text',
'Another line with INC1054789456 and more details',
'Text without an incident number',
'INC1034895678 incident at the start'
]
}
# Create DataFrame
df = pd.DataFrame(data)
# Function to extract incident number
def extract_incident_number(text):
match = re.search(r'INC\d{10}', text)
if match:
return match.group(0)
return "No Incident Number"
# Apply the function to the DataFrame
df['Incident Number'] = df['RTSK Worknote'].apply(extract_incident_number)
# Display the updated DataFrame
print(df)
Explanation:
Import Necessary Libraries:
import pandas as pdandimport re.Sample Data: Create a DataFrame with sample data for demonstration purposes.
Define a Function:
extract_incident_numberto find the incident number using a regular expression:re.search(r'INC\d{10}', text): Searches for the pattern "INC" followed by exactly 10 digits.match.group(0): Returns the matched incident number.If no match is found, it returns "No Incident Number".
Apply the Function: Use
applyto apply the function to each row in theRTSK Worknotecolumn.Display the Results: Print the updated DataFrame with a new column,
Incident Number.
This code will extract incident numbers that start with "INC" followed by a 10-digit number and add them to a new column in the DataFrame. If no such incident number is found, it will return "No Incident Number".
No comments