Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add column value in panda data frame if certain condition is met

I have two lists:

    required=["api.gotinder.com","data.gotinder.com"]
    not_required=["graph.facebook.com","launches.appsflyer.com"]

If the value in requested_server_name column is in required list then it should be labeled as Required if the value in requested_server_name column is in not_required list then it should be labeled as Not_Required

What I am doing at the moment:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for j in required:
    conditions = [
        (frame['requested_server_name'] == j)
        ]
    values = ["Required"]
    frame['label'] = np.select(conditions, values)

for k in not_required:
    conditions = [
        (frame['requested_server_name'] == k)
        ]
    values = ["Not_Required"]
    frame['label'] = np.select(conditions, values)

What am I doing wrong?? It only populates the label for launches.appsflyer.com.

>Solution :

Create a dict then reverse keys and values to create a mapping dict. Finally use replace to apply the mapping:

d = {'required': ["api.gotinder.com", "data.gotinder.com"],
     'not_required': ["graph.facebook.com", "launches.appsflyer.com"]}

MAP = {v: k for k, l in d.items() for v in l}

frame['label'] = frame['requested_server_name'].replace(MAP)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading