Data Automation & Cleaning Beginner

Bulk Validate PAN Card & GSTIN Numbers with Checksum in Python (Regex)

Clean vendor master CSVs in Python by validating 10-character PAN and 15-character GSTIN formats, state codes, and entity types using compiled Regex.

#pandas

Problem Statement

Invalid vendor GSTIN or PAN entries in ERP master files cause e-Invoicing and TDS filing rejections. This vectorized Pandas script validates thousands of tax IDs instantaneously.

Formula / Core Logic

PAN Regex: ^[A-Z]{5}[0-9]{4}[A-Z]{1}$ GSTIN Regex: ^(0[1-9]|[1-2][0-9]|3[0-8])[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$

Installation

pip install pandas
validate-indian-pan-gstin-regex-python-script.py
import pandas as pd

PAN_REGEX = r'^[A-Z]{5}[0-9]{4}[A-Z]{1}$'
GSTIN_REGEX = r'^(0[1-9]|[1-2][0-9]|3[0-8])[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$'

def validate_tax_ids(df, pan_col='PAN', gst_col='GSTIN'):
    df = df.copy()
    df['Valid_PAN'] = df[pan_col].astype(str).str.strip().str.upper().str.match(PAN_REGEX)
    df['Valid_GSTIN'] = df[gst_col].astype(str).str.strip().str.upper().str.match(GSTIN_REGEX)
    df['Embedded_PAN_Match'] = df[gst_col].astype(str).str[2:12] == df[pan_col].astype(str).str.strip().str.upper()
    return df

Get the Complete 100+ Python Quant & Automation Bundle

Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).

Buy Full Bundle on Gumroad ($19) →

Frequently Asked Questions

Q: What does `Embedded_PAN_Match` verify?

Characters 3 to 12 of every valid GSTIN must exactly match the entity's 10-character PAN.