Home > Mobile >  Create column based on existing column in dataframe and lists
Create column based on existing column in dataframe and lists

Time:09-01

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

CodePudding user response:

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

CodePudding user response:

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
  • Related