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

Make new column based on values in other columns pandas

I want to introduce a new col in df based on other col values.
If c1-c3 cols have only 1 unique value then that unique value will go into c4 col.
If c1-c3 cols have two different values then "both" will go into c4 col.
NaN should not be considered as a valid value. Only c2 and c3 have a few NaNs.

Minimal example:

df = pd.DataFrame({
                     "c1": ["left", "right", "right", "left", "left","right"], 
                     "c2": ["left", "right", "right", "right", "NaN","right"], 
                     "c3": ["NaN", "NaN", "left", "NaN", "left","right"]})

Required df:

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

answerdf = pd.DataFrame({
                     "c1": ["left", "right", "right", "left", "left","right"], 
                     "c2": ["left", "right", "right", "right", "NaN","right"], 
                     "c3": ["NaN", "NaN", "left", "NaN", "left","right"], 
                        "c4":["left", "right", "both", "both", "left","right"] })

>Solution :

import pandas as pd
import numpy as np

df = pd.DataFrame({
    "c1": ["left", "right", "right", "left", "left", "right"],
    "c2": ["left", "right", "right", "right", np.nan, "right"],
    "c3": [np.nan, np.nan, "left", np.nan, "left", "right"]
})

def worker(row):
    if "left" in row.values and "right" in row.values:
        return "both"
    if "left" in row.values:
        return "left"
    if "right" in row.values:
        return "right"
    return np.nan

df["c4"] = df[["c1", "c2", "c3"]].apply(worker, axis=1)

This returns nan if neither left nor right is given and might be easier to understand

Output

    c1  c2  c3  c4
0   left    left    NaN     left
1   right   right   NaN     right
2   right   right   left    both
3   left    right   NaN     both
4   left    NaN     left    left
5   right   right   right   right
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