Home > Mobile >  Check if value column A in B python
Check if value column A in B python

Time:09-06

I want to add a new column 'check' with the following condition:

  • 'Suppression total' and 'Sup-SDM'.

OR

  • Suppression partiel and Franc SUP - Geisi

Dataframe:

Date of event Type Info
2021-12-08 Sup_EF - SUP - SDM 2021-12-08 16:47:51.0-Suppression totale
2021-12-15 Modif_EF - SUP - SDM 2021-12-08 16:47:51.0-Creation
2021-12-31 Sup_EF - SUP - Geisi 2021-12-08 16:47:51.0-Suppression totale
2021-12-17 Modif_EF - Franc SUP - Geisi 2021-12-17 10:50:40.0-Suppression partiel

Desired output:

Date of event Type Info Check
2021-12-08 Sup_EF - SUP - SDM 2021-12-08 16:47:51.0-Suppression total Correct
2021-12-15 Modif_EF - SUP - SDM 2021-12-08 16:47:51.0-Creation Fail
2021-12-31 Sup_EF - SUP - Geisi 2021-12-08 16:47:51.0-Suppression total Fail
2021-12-17 Modif_EF - Franc SUP - Geisi 2021-12-17 10:50:40.0-Suppression partiel Correct

Code:

df['check'] = np.where((df.Type.str.contains('SUP - SDM') & df.Info.str.contains('Suppression total') & (df['Date of event] in df.Info))
                       | (df.Type.str.contains('Franc SUP - Geisi') & (df.Info.str.contains('Suppression partiel'))&& (df['Date of event] in df.Info))),'correct','fail')

But error 'Series' objects are mutable, thus they cannot be hashed

CodePudding user response:

For test if exist substring per 2 columns use list comprehension:

m = [a in b for a, b in zip(df['Date of event'].astype(str), df['Info'])]
df['check'] = np.where((df.Type.str.contains('SUP - SDM') & df.Info.str.contains('Suppression total') & m) |
                       (df.Type.str.contains('Franc SUP - Geisi') & df.Info.str.contains('Suppression partiel') &m),'correct','fail')

print (df)
  Date of event                           Type  \
0   2021-12-08             Sup_EF - SUP - SDM    
1   2021-12-15           Modif_EF - SUP - SDM    
2   2021-12-31           Sup_EF - SUP - Geisi    
3   2021-12-17   Modif_EF - Franc SUP - Geisi    

                                        Info    check  
0   2021-12-08 16:47:51.0-Suppression totale  correct  
1             2021-12-08 16:47:51.0-Creation     fail  
2   2021-12-08 16:47:51.0-Suppression totale     fail  
3  2021-12-17 10:50:40.0-Suppression partiel  correct  
  • Related