4

I have a column let's say 'Match Place' in which there are entries like 'MANU @ POR', 'MANU vs. UTA', 'MANU @ IND', 'MANU vs. GRE' etc. So my columns have 3 things in its entry, the 1st name is MANU i.e, 1st country code, 2nd is @/vs. and 3rd is again 2nd country name. So what I wanna do is if '@' comes in any entry of my column I want is to be changed to 'away' and if 'vs.' comes in replace whole entry to 'home' like 'MANU @ POR' should be changed to 'away' and 'MANU vs. GRE' should be changed to 'home'

although I wrote some code to do so using for, if, else but it's taking a way too long time to compute it and my total rows are 30697 so is there any other way to reduce time below I'm showing you my code pls help

for i in range(len(df)):
    if is_na(df['home/away'][i]) == True:
        temp = (df['home/away'][i]).split()
        if temp[1] == '@':
            df['home/away'][i] = 'away'
        else:
            df['home/away'][i] = 'home
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • since your first word is always MANU, Your 6th element of string either contains '@' or 'v'. You can use check df['home/away']][i][5] to fasten the process rather than using split. – PaxPrz Jul 20 '19 at 14:51

4 Answers4

3

You can use np.select to assign multiple conditions:

s=df['Match Place'].str.split().str[1] #select the middle element
c1,c2=s.eq('@'),s.eq('vs.') #assign conditions
np.select([c1,c2],['away','home']) #assign this to the desired column
#array(['away', 'home', 'away', 'home'], dtype='<U11')
anky
  • 74,114
  • 11
  • 41
  • 70
2

You can use .str.contains(..) [pandas-doc] to check if the string contains an @, and then use .map(..) [pandas-doc] to fill in values accordingly. For example:

>>> df
          match
0    MANU @ POR
1  MANU vs. UTA
2    MANU @ IND
3  MANU vs. GRE
>>> df['match'].str.contains('@').map({False: 'home', True: 'away'})
0    away
1    home
2    away
3    home
Name: match, dtype: object
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
2

use np.where to with contains to check any substring exist or not

import numpy as np
df = pd.DataFrame(data={"col1":["manu vs. abc","manu @ pro"]})
df['type'] = np.where(df['col1'].str.contains("@"),"away","home")
        col1       type
0   manu vs. abc    home
1   manu @ pro      away
tawab_shakeel
  • 3,701
  • 10
  • 26
2

A fun usage of replace more info check link

df['match'].replace({'@':0},regex=True).astype(bool).map({False: 'away', True: 'home'})
0    away
1    home
2    away
3    home
Name: match, dtype: object
BENY
  • 317,841
  • 20
  • 164
  • 234