Python code to search incident number

 

Sample Code to Extract Incident Numbers:

python
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:

  1. Import Necessary Libraries: import pandas as pd and import re.

  2. Sample Data: Create a DataFrame with sample data for demonstration purposes.

  3. Define a Function: extract_incident_number to 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".

  4. Apply the Function: Use apply to apply the function to each row in the RTSK Worknote column.

  5. 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

Theme images by tjasam. Powered by Blogger.