'Unable to replace an existing column basis on another column

Below are two dataframes. The dataframe df1 is a cleanfile which has to be used as the mapping file to another dataframe df2 which has uncleaned information.

    df1=pd.DataFrame({'Make': {0: 'ASHOK LEYLAND', 1: 'ASTON MARTIN', 2: 'ASTON MARTIN'},
     'Model': {0: 'STILE', 1: 'DB9', 2: 'RAPIDE'},
     'Variant': {0: 'LE 7 STR', 1: 'VOLANTE', 2: 'LUXE'},
     'Fuel': {0: 'DIESEL', 1: 'PETROL', 2: 'PETROL'}})

    df2=pd.DataFrame({'Make': {0: 'ASHOK LEYLANDSTILELE 7 STR',
      1: 'ASTON MARTINDB9VOLANTE',
      2: 'ASTON MARTINRAPIDELUXE'},
     'Model': {0: 'STILELE 7 STR', 1: 'DB9VOLANTE', 2: 'RAPIDELUXE'},
     'Variant': {0: 'LE 7 STRSTILE', 1: 'VOLANTEDB9', 2: 'LUXERAPIDE'},
     'Fuel': {0: 'Dieseel', 1: 'Dieseel', 2: 'PETROLjlljlj'}})

I have used the below code to clean the 'Make' column of df2 basis on the 'Make' column of df1. However i dont get any result on my new column 'Make_new'.Below is the code:

    df2['Make_new'] = df2['Make'].apply(lambda v: [Make for Make in df1 if Make in ('ASHOK','ASTON')])


Solution 1:[1]

I don't see why you would need df1 at all. The "cleaning" logic is pretty simple. This will do the trick.

def clean(row):
    if 'ASHOK' in row.Make:
        return 'ASHOK LEYLAND'
    if 'ASTON' in row.Make:
        return 'ASTON MARTIN'
    return row.Make

df2['cleaned'] = df2.apply(clean, axis=1)

Solution 2:[2]

You can't implement someone else's trait for someone else's type. This is to prevent conflicting definitions. What if you and I BOTH write a crate that defines addition for vec, and someone else uses both the crates? Whose implementation should be used?

What you can do is wrap vec in your own type and define Add for that type.

struct MyVec<T> { 
  fn new(from: Vec<T>) { ...
}
impl Add for MyVec { ... }

If you want to make access to the underlying Vec transparent (like it is for Box Arc Mutex etc.) you can impl deref for your type.

Something like this will allow you to easily call all the vec methods on your new type.

use std::ops::Deref;

struct MyVec<T> {
    value: T
}
impl<T> Deref for MyVec<T> {
    type Target = Vec<T>;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 alec_djinn
Solution 2 nlta