1

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
1
  • So for nztracks is assigned NZ,HK, SING ? Not only NZ ? Commented Aug 31, 2022 at 6:37

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.