ambigus9
ambigus9

Reputation: 1569

How to map a dataframe with a dictionary using a function?

I would like to apply a function while mapping a dataframe with the key's from a dictionary

col1 col2 
foo 0 
baa 1 
wen 2

dict: {
'foo':a,
'baa':b,
'wen':c
}

def function(value1,value2):
    return value1+value2

output

col1 col2
fooa 0
baab 1
wenc 2

thanks

Upvotes: 0

Views: 63

Answers (1)

Chris
Chris

Reputation: 199

df["col1"] =  df["col1"].apply(lambda x: x + (dict[x]))

Should do the trick

.apply will let you apply a function to the whole column, and you can make a fast, anonymous function with lambda. Here's the docs on this: pandas.Series.apply

Upvotes: 1

Related Questions