I have a this array of dictionaries:
jsn = [{'ID' : '01', 'Item' : 'Apple', 'salesman' :'Johnny'},
{'ID': '02', 'Item': 'carrot', 'salesman':'Patricia'},
{'ID': '04', 'Item' : 'airplane', 'salesman' : 'Eddgard'},
{'ID' : '05', 'item' : 'iron', 'salesman' : 'Bettany'}]
and i also have this dataframe:
df1 = pd.DataFrame([["1", "apple"], ["2", "mango"],
["3", "melon"], ["4", "watermelon"],
["5", "strawberry"]],columns=["ID", "Item"])
i want to change all the "Item" Values from the jsn array based on the df1 "Item" values.
I’ve tried this approach
for k in df1:
for i in jsn1:
i.update({'Item': f"{k}"})
But it didn’t work as i’ve expected.
4
Answers
You can do this:
Output:
What about:
Updated
jsn
:You can create a
dict
basedf1
. then update eachdict
injsn
like the below:Try using merge
Output: