Create column based on existing column in dataframe and lists

Question:

I have a dataframe with multiple columns. One of the columns, ‘TName’, has a different track names. I would like to create a new column for my dataframe which has the country of each racetrack.

My code (idk what to do with the last two lines):

austracks = ['Ascot', 'Balnarring']
nztracks = ['Te Aura']
hktracks = ['Sha Tin']
singtracks = ['Singapore']

df['Country'] = df['TName'] 
(["AUS" for c in austracks] + ["NZ" for c in nztracks] + ["HK" for c in nztracks] + ["SING" for c in nztracks])

Desired dataframe:

TName        Country
Ascot          AUS
Balnarring     AUS
Te Aura        NZ
Asked By: onthepunt

||

Answers:

Use Series.map if need for each list assign only one country:

austracks = ['Ascot', 'Balnarring']
nztracks = ['Te Aura']

d = {**dict.fromkeys(austracks,'AUS'), **dict.fromkeys(nztracks,'NZ')}

df['Country'] = df['TName'].map(d)
print (df)
        TName Country
0       Ascot     AUS
1  Balnarring     AUS
2     Te Aura      NZ
Answered By: jezrael

You an also use user defined function:

import pandas as pd

austracks = ['Ascot', 'Balnarring']
nztracks = ['Te Aura']
hktracks = ['Sha Tin']
singtracks = ['Singapore']

df = pd.DataFrame(data=austracks+nztracks+hktracks+singtracks, columns=["TName"])

def get_country_for_track(tname):
    if tname in austracks:
        return "AUS"
    elif tname in nztracks:
        return "NZ"
    elif tname in hktracks:
        return "HK"
    elif tname in singtracks:
        return "SING"

df["Country"] = df.apply(lambda row: get_country_for_track(row["TName"]), axis=1)
print(df)

Output:

        TName Country
0       Ascot     AUS
1  Balnarring     AUS
2     Te Aura      NZ
3     Sha Tin      HK
4   Singapore    SING
Answered By: Athar Khan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.