skip to Main Content

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


  1. You can do this:

    for dict_ in jsn:
        dict_["Item"] = df1[df1.ID.astype(int) == int(dict_["ID"])].Item.values[0]
    
    jsn
    

    Output:

    [{'ID': '01', 'Item': 'apple', 'salesman': 'Johnny'},
     {'ID': '02', 'Item': 'mango', 'salesman': 'Patricia'},
     {'ID': '04', 'Item': 'watermelon', 'salesman': 'Eddgard'},
     {'ID': '05', 'Item': 'strawberry', 'salesman': 'Bettany'}]
    
    Login or Signup to reply.
  2. What about:

    ref = df1.set_index('ID')['Item']
    
    for d in jsn:
        d['Item'] = ref.get(str(int(d['ID'])), d.setdefault('Item', None))
        # or
        # d['Item'] = ref.get(d['ID'].lstrip('0')), d.setdefault('Item', None))
    

    Updated jsn:

    [{'ID': '01', 'Item': 'apple', 'salesman': 'Johnny'},
     {'ID': '02', 'Item': 'mango', 'salesman': 'Patricia'},
     {'ID': '04', 'Item': 'watermelon', 'salesman': 'Eddgard'},
     {'ID': '05', 'Item': 'strawberry', 'salesman': 'Bettany'}]
    
    Login or Signup to reply.
  3. You can create a dict base df1. then update each dict in jsn like the below:

    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'}]
    df1 = pd.DataFrame( [["2", "mango"], ["3", "melon"], ["4", "watermelon"], ["5", "strawberry"]],columns=["ID", "Item"] )
    
    
    dct = dict(zip(df1['ID'].astype(int), df1['Item']))
    for j in jsn:
            j['Item'] = dct.get(int(j['ID']), j['Item'])
    print(jsn)
    

    [{'ID': '01', 'Item': 'Apple', 'salesman': 'Johnny'},
     {'ID': '02', 'Item': 'mango', 'salesman': 'Patricia'},
     {'ID': '04', 'Item': 'watermelon', 'salesman': 'Eddgard'},
     {'ID': '05', 'Item': 'strawberry', 'salesman': 'Bettany'}]
    
    Login or Signup to reply.
  4. Try using merge

    import pandas as pd
    
    df1 = pd.DataFrame( [["1", "apple"], ["2", "mango"], ["3", "melon"], ["4", "watermelon"], ["5", "strawberry"]],columns=["ID", "Item"] )
    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'}]
    df2 = pd.DataFrame(jsn)
    
    df1["Item"] = df1["Item"].str.lower()
    df2["Item"] = df2["Item"].str.lower()
    
    df1.merge(df2, how='left', on='Item')
    

    Output:

    ID_x Item ID_y salesman
    0 1 apple 01
    1 2 mango NaN
    2 3 melon NaN
    3 4 watermelon NaN
    4 5 strawberry NaN
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search