I have a dictionary:
x = {'[a]':'(1234)', '[b]':'(2345)', '[c]':'(xyzad)'}
and a dataframe
df = pd.DataFrame({'q':['hey this is [a]', 'why dont you [b]', 'clas is [c]']})
I want to append the values from dictionary to the corresponding keys.
My expected output is:
q
0 hey this is [a](1234)
1 why dont you [b](2345)
2 clas is [c](xyzad)
Here's my solution:
x = {k: k+v for k,v in x.items()}
def das(data):
for i in x.keys():
if i in data:
data = data.replace(i, x[i])
return data
df['q'] = df['q'].apply(lambda x: das(x))
print(df)
Is there a way to improve this?
I have to update a dictionary by appending a key infront of values. Then I use apply
to replace the values.
I am looking for more efficient solution.
[]
? \$\endgroup\$