0

I have Df like this

      Name     PAN
 0    x     BBDFW7894Q 
 1    s         
 2    A     QWE7892E

i want df like this After validation PAN number

     Name     PAN           PAN_Status
 0    x     BBDFW7894Q      Valid PAN number
 1    s                     PAN is not present
 2    A     QWE7892E        Invalid PAN number

i trying with below logic

df["PAN_Status"] = ["Valid PAN number" if re.match(r'^[A-Z]{5}[0-9]{4}[A-Z]$',ele)\
                    else ("PAN is not present"if ele==' ' else "Not Valid PAN") for ele in 

but i want a Function for this

1 Answer 1

1

Use numpy.select with Series.str.match and for second condition is tested missing values or emty strings:

m1 = df["PAN"].str.match(r'^[A-Z]{5}[0-9]{4}[A-Z]$', na=False)
m2 = df["PAN"].str.strip().eq('') | df['PAN'].isna()

df["PAN_Status"] = np.select([m1, m2], 
                             ['Valid PAN number','PAN is not present'], 
                             'Not Valid PAN')

Solution with function:

def test(ele):
    if (ele.strip() =='') or pd.isna(ele):
        return "PAN is not present"
    elif re.match(r'^[A-Z]{5}[0-9]{4}[A-Z]$',ele):
        return "Valid PAN number"
    else:
        return "Not Valid PAN"

df["PAN_Status"] = df["PAN"].apply(test)
print (df)
  Name         PAN          PAN_Status
0    x  BBDFW7894Q    Valid PAN number
1    s         NaN  PAN is not present
2    A    QWE7892E       Not Valid PAN
Sign up to request clarification or add additional context in comments.

1 Comment

sir you are a always right but ,I want a function like 'def (): code return'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.