add suffix based on multiple conditions from string values in another column

Question:

I would like to add a suffix to strings in one column when a condition is met in another column. If a value is present in "Market" column, "Symbol" column corresponding value is updated to include current ticker but I would like to add a suffix to it representing its market place. I guess I could create multiple masks and change multiple values with multiple lines of codes for each value but I was wondering if there exist a more elegant way of doing this in one operation.

This is what I tried :

conditions = [ (df['Market'].str.contains("Oslo")), (df['Market'].str.contains("Paris")), (df['Market'].str.contains("Amsterdam")), (df['Market'].str.contains("Brussels")), (df['Market'].str.contains("Dublin"))   ] 
values = [str+'.OL', str+'.PA', str+'.AS', str+'.BR', str+'.IR'] 
df['Symbol'] = np.select(conditions, values)
print(df)

I get an error :

unsupported operand type(s) for +: ‘type’ and ‘str’

any help welcome

added after KingOtto’s answer…

the data frame :

|  Name             | Symbol   | Market                 |
|:------------------|:---------|:-----------------------|
|  1000MERCIS       | ALMIL    | Euronext Growth Paris  |
|  2020 BULKERS     | 2020     | Oslo Børs              |
|  2CRSI            | 2CRSI    | Euronext Paris         |
|  2MX ORGANIC      | 2MX      | Euronext Paris         |
|  2MX ORGANIC BS   | 2MXBS    | Euronext Paris         |
|  5TH PLANET GAMES | 5PG      | Euronext Expand Oslo   |
|  A TOUTE VITESSE  | MLATV    | Euronext Access Paris  |
|  A.S.T. GROUPE    | ASP      | Euronext Paris         |
|  AALBERTS NV      | AALB     | Euronext Amsterdam     |
|  AASEN SPAREBANK  | AASB     | Euronext Growth Oslo   |
|  AB INBEV         | ABI      | Euronext Brussels      |
|  AB SCIENCE       | AB       | Euronext Paris         |
|  ABATTOIR         | -        | Euronext Expert Market |
|  ABC ARBITRAGE    | ABCA     | Euronext Paris         |
|  ABEO             | ABEO     | Euronext Paris         |

The code to create a new df with a suffix column and then merge these values with the symbol column to obtain a ticker correctly understood by Yahoo Finance to be queried.

suffix_list = pd.DataFrame({'Market': ['Euronext Growth Oslo','Euronext Expand Oslo', 'Oslo Børs', 'Euronext Brussels','Euronext Growth Brussels','Euronext Paris', 'Euronext Access Paris','Euronext Growth Paris', 'Euronext Lisbon', 'Euronext Access Lisbon', 'Euronext Dublin', 'Euronext Growth Dublin', 'Euronext Brussels', 'Euronext Access Brussels','Euronext Amsterdam'], 'suffix':['.OL','.OL','.OL','.BR','.BR','.PA','.PA','.PA','.LI','.LI','.IR','.IR','.BR','.BR','.AS']})
new_df = pd.merge(df, suffix_list, how='left', on='Market')
new_df['Ticker']=new_df['Symbol']+new_df['suffix']

The new dataframe containing the Ticker column:

|  Name             | Symbol   | Market                 | suffix   | Ticker   |
| :-----------------|:---------|:-----------------------|:---------|:---------|
|  1000MERCIS       | ALMIL    | Euronext Growth Paris  | .PA      | ALMIL.PA |
|  2020 BULKERS     | 2020     | Oslo Børs              | .OL      | 2020.OL  |
|  2CRSI            | 2CRSI    | Euronext Paris         | .PA      | 2CRSI.PA |
|  2MX ORGANIC      | 2MX      | Euronext Paris         | .PA      | 2MX.PA   |
|  2MX ORGANIC BS   | 2MXBS    | Euronext Paris         | .PA      | 2MXBS.PA |
|  5TH PLANET GAMES | 5PG      | Euronext Expand Oslo   | .OL      | 5PG.OL   |
|  A TOUTE VITESSE  | MLATV    | Euronext Access Paris  | .PA      | MLATV.PA |
|  A.S.T. GROUPE    | ASP      | Euronext Paris         | .PA      | ASP.PA   |
|  AALBERTS NV      | AALB     | Euronext Amsterdam     | .AS      | AALB.AS  |
|  AASEN SPAREBANK  | AASB     | Euronext Growth Oslo   | .OL      | AASB.OL  |
|  AB INBEV         | ABI      | Euronext Brussels      | .BR      | ABI.BR   |
|  AB INBEV         | ABI      | Euronext Brussels      | .BR      | ABI.BR   |
|  AB SCIENCE       | AB       | Euronext Paris         | .PA      | AB.PA    |
|  ABATTOIR         | -        | Euronext Expert Market | nan      | nan      |
|  ABC ARBITRAGE    | ABCA     | Euronext Paris         | .PA      | ABCA.PA  |
|  ABEO             | ABEO     | Euronext Paris         | .PA      | ABEO.PA  |
Asked By: Zen4ttitude

||

Answers:

You need to proceed in 3 steps

  1. You need to define an exhaustive suffix_list – a dictionary that holds information only once for each market

    suffix_list = pd.DataFrame({'Market': ['Oslo', 'Paris'], 'suffix':['OL','PA']})

  2. You want to merge the suffix_list into your existing dataframe as a new column – one command for all markets (for each market that has a suffix in the list, you add that suffix):

    pd.merge(df, suffix_list, how='left', on='Market')

  3. Now that you have the 2 columns 'value' and 'suffix' next to each other for all rows, you can apply 1 single operation for all rows

    str('value')+'suffix'

Answered By: KingOtto