Home > Software design >  if statement/conditions to check a dataframe column for a string in Python
if statement/conditions to check a dataframe column for a string in Python

Time:12-22

I have a column with text like "Dublin 2", "Dublin 4" etc in it. There are up to 24 Dublins that I need to check for.

I want to do something like:

if df["Postcode"] == "Dublin 2":
    df["Popularity"] == 10
        
elif df["Postcode"] == "Dublin 3":
    df["Popularity"] == 3

etc

I have tried using conditions and np.select, it works but that's not feasible for the number of Dublins I have.

conditions = [

    df['Dublin Postcode'].str.contains('Dublin 1'),
    df['Dublin Postcode'].str.contains('Dublin 2'),
    
    ]

values = [10,3]

df['Popularity'] = np.select(conditions, values, default=5) 

Is there a smarter way of getting it to work? I can't see the wood for the trees at this stage!

CodePudding user response:

you can create a dict from the string to the population like:

d = {'Dublin 2': 10,...}

then you can use applay like this:

df["Popularity"] = df["Postcode"].apply(lambda x: d[x])

CodePudding user response:

I think you can use apply:

import pandas as pd

postcode_to_popularity = {"Dublin 2": 10, "Dublin 3": 3} # add your other mappings here
df = pd.DataFrame({"Postcode": ["Dublin 2", "Dublin 3", "Unknwown", "Dublin 2"]}) # here you use your own dataframe
df["Popularity"] = df["Postcode"].apply(lambda postcode: postcode_to_popularity.get(postcode))
print(df)

That will give you this:

   Postcode  Popularity
0  Dublin 2        10.0
1  Dublin 3         3.0
2  Unknwown         NaN
3  Dublin 2        10.0

I hope this answers your question !

  • Related