Question: I have two pandas columns, where I first compare the two columns and then replace an old string with a new one. 

My data: 

```
shopping  on_List
Banana    1 
Apple     0
Grapes    1 
None      0
Banana    1
Nuts      0
Lemon     1

```

In order to compare the two I have done the following:

```
results = []
for shopping, on_list in zip(df.shopping, df.on_list):
    if shopping != 'None' and on_list == 1:
        items = shopping

    if items == 'Banana':
        re = items.replace('Banana', 'Bananas')
    elif items == 'Lemon':
        re = items.replace('Lemon', 'Lemons')
    elif items == 'Apples':
        re= items.replace('Apple','Apples')

results.append(re)
print(results) 
Output: ['Bananas','Lemons', 'Apples']

```

Ideally I would like to return a new column that replaces my new values with old ones in the 'shopping' column: 

This is my desired output, but unfortunately my new list (results) is not the same length as the current df: 

```
shopping 
Bananas
Apples 
Grapes 
None 
Bananas 
Nuts 
Lemons

```

Answer: 
I suggest create dictionary for mapping and replace filtered values:

```
d = {'Banana':'Bananas', 'Lemon':'Lemons', 'Apple':'Apples'}

mask = df['on_List'].eq(1) & df['on_List'].notnull()
df['shopping'] = df['shopping'].mask(mask, df['shopping'].map(d)).fillna(df['shopping'])
#slowier solution
#df['shopping'] = df['shopping'].mask(mask, df['shopping'].replace(d))
print (df)
  shopping  on_List
0  Bananas        1
1    Apple        0
2   Grapes        1
3     None        0
4  Bananas        1
5     Nuts        0
6   Lemons        1

```
```
val = []
for i in range(len(df)):
    if df["shopping"][i] != None and df["on_List"][i] == 1:
        if df["shopping"][i] == "Banana":
            val.append("Bananas")
        elif df["shopping"][i] == "Lemon":
            val.append("Lemons")
        elif df["shopping"][i] == "Apple":
            val.append("Apples")
    else:
        val.append("None")

df["Result"] = pd.Series(val)

```