0

I have a 2 lists with data which I want to match and receive 3rd list with 'True' or 'False'. However I need to create a special match. See my code below.

import pandas as pd

list1 = ['food', 'food', 'item']
list2 = ['apple fr', 'cherry fr', 'banana fr']
my_list1 = list(map(str, list1))
my_list2 = list(map(str, list2))
for i in range(len(my_list2)):
    if my_list2[i] in [s for s in my_list2 if "fr" in s]:
        my_list2[i] = 'food'
result = []
for i in range(int(len(my_list1))):
    result.append(my_list1[i] == my_list2[i])
my_data1 = pd.DataFrame({'a': list1, 'b': list2, 'check': result})
print(my_data1) 

Output:

      a          b  check
0  food   apple fr   True
1  food  cherry fr   True
2  item  banana fr  False

Basically, I want to match elements of list using next pattern: if elem from list2 has 'fr' than it should return True on 'food' element from list1.

As you can see I already have a sort of solution where I am copying lists , editing them and comparing edited copies. However this approach is not the best since it is really memory consuming. Maybe it is possible to play with 'if' condition rather then manipulating data. Is there is any better way to do it? Thanks

Zalman
  • 33
  • 2
  • `my_list1 = list(map(str, list1))` What is the point of this? The items in `list1` are obviously strings already. – John Gordon Jun 14 '23 at 00:55

2 Answers2

0

If I understand the check correctly, it is simply the question "does element from list1 equal 'food' (df['a'] == 'food') and (&) does element from list2 end in ' fr' (df['b'].str.endswith(' fr')).

import pandas as pd

list1 = ['food', 'food', 'item', 'food']
list2 = ['apple fr', 'cherry fr', 'banana fr', 'berry us']
df = pd.DataFrame([list1, list2]).T
df.columns = ['a', 'b']
df['check'] = (df['a'] == 'food') & (df['b'].str.endswith(' fr'))
print(df)

prints

      a          b  check
0  food   apple fr   True
1  food  cherry fr   True
2  item  banana fr  False
3  food   berry us  False

Comment: The endswith(' fr') of course assumes that the elements of list2 should only match if they end with ' fr', not however if they contain 'fr' anywhere, which is what if "fr" in s checks for (e.g. fr in 'jackfruit' would return True, though I assume you would not want this).

Michael Hodel
  • 2,845
  • 1
  • 5
  • 10
0

You can also create your check column this way, making use of:

check = [list1[i] == ("food" if ("fr" in s) else "item") for i, s in enumerate(list2)]
iGian
  • 11,023
  • 3
  • 21
  • 36