I am trying to match a string in a cell and if it is present I should be able to write a specific string in new cell/column. For example:
This is my xlsx file:
|UserPrincipalName |Contact |Age |Container |
|:-------------------------|:------:|:-----:|:--------------------------:|
|John.doe |Email |23 |\Location\EMEA\France\User |
|Jane.doe |Phone |25 |\Location\EMEA\Germany\User|
|Jane12 |Phone |25 |\Location\EMEA\Italy\User |
|Jane1322 |Phone |25 |\Location\EMEA\Belgium\User|
|Feng.Main |SMS |21 |\Location\APAC\China\User |
|serviceaccount |Email | |\Location\Service Accounts |
Expected Output:
|UserPrincipalName |Contact |Age |Container |Region |
|:-------------------------|:------:|:-----:|:-----------------:|:--------------|
|John.doe |Email |23 |\Location\EMEA\France\User |EMEA North |
|Jane.doe |Phone |25 |\Location\EMEA\Belgium\User|EMEA South |
|Jane12 |Phone |25 |\Location\EMEA\Germany\User|EMEA South |
|Jane1322 |Phone |25 |\Location\EMEA\Italy\User |EMEA North |
|Feng.Main |SMS |21 |\Location\APAC\China\User |APAC |
|serviceaccount |Email | |\Location\Service Accounts |Service Account|
My Code:
df = pd.read_excel('C:\\Users\\Desktop\\OpTest.xlsx')
df['Region'] = df[df['Container'].str.contains(r'\\EMEA\\')]
df = df.to_excel('C:\\Users\\Desktop\\MFAOpTest.xlsx')
My Output: enter image description here