user14784453
user14784453

Reputation:

Create function from Logic for Dataframe

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

Upvotes: 0

Views: 52

Answers (1)

jezrael
jezrael

Reputation: 863791

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

Upvotes: 1

Related Questions