Home > OS >  create new column from dictionary key if condition matches in values
create new column from dictionary key if condition matches in values

Time:12-14

I have a dataframe and a dictionary

df
col1 col2
a      a1
b      b1
c      c1
d      d1
dict
company = {
    'abcd': [a1],
    'efgh': [b1],
    'rewr': [c1],
    'fsdf': [d1]
}

I need to create new column from dictionary key if the value matches the column in df so that my output df is:

col1 col2  new_col
a      a1   abcd
b      b1   efgh
c      c1   rewr
d      d1   fsdf

my current loop for that is below but is there a more pythonic way of doing this:

def find_value(v):
    found = False
    for k in dic.keys():
        if v in dic[k]: 
            return k
    if not found: return None

df['new_col'] = dataset['col2'].apply(find_value)

CodePudding user response:

If you are looking for a way to streamline find_value (I'm inclined to call it find_key since that is what you return) here is a snippet that leverages the items method from dictionary. Note, found is not needed since it will always return the key if target_value is found otherwise it will return None. Strictly speaking, the return None line is not needed since Python functions return None by default, but it makes the code more clear in this case to be explicit.

def find_key(target_value):
    for k, v in dic.items():
        if v == target_value:
            return k
    return None

CodePudding user response:

df['new_col'] = df['col2']
df.replace({"new_col": {v[0]: k for k, v in company.items()}})
Output:
    col1    col2    new_col
0   a       a1      abcd
1   b       b1      efgh
2   c       c1      rewr
3   d       d1      fsdf

Or a oneliner:

df['new_col'] = df.col2.replace({v[0]: k for k, v in company.items()})

Reverse / invert a dictionary mapping
Remap values in pandas column with a dict

For the values that did not match, they will be addeed with the same values as col2, hence can be nullified with:

df['new_col'][df.new_col == df.col2] = None

CodePudding user response:

You can use pd.Series.map on reversed dictionary.

df['new_col'] = df['col2'].map({v[0]:k for k,v in company.items()})
  • Related