-1

I appreciate the help.

I have two data sets:

models = pd.DataFrame({"manufacturer":['Nokia', 'Samsung', 'Motorola', 'Nokia', 'Alcatel', 'Nokia 5'],
               "model":['3310', 'S4', 'Moto G', 'N1', 'Pixy', 'Nokia 5']})

data = pd.DataFrame({"title":["Brand New Nokia 3310", "Old Samsung S4", "Cool Motorola Moto G", 'New Alcatel', 'Old Nokia 5'],
                "manufacturer":['Nokia', "Samsung", "Motorola", 'Alcatel', 'Nokia'],
                "model":["placeholder", "placeholder", "placeholder", "placeholder", "placeholder"]})

I need to populate model value in data['model'] by condition that the string in title contains model string, otherwise the value in that column should remain placeholder.

I tried to do it by using a list comprehension but it's not working.

mod = models["model"].tolist()
title = data['title']
data['model'] = pd.Series([m for m in mod for t in title if m in]

I also tried by using "str.contains" which is pandas method:

for m in mod:
    if title.str.contains(m):
        data['model'] = m

But that also is not working as i expected. Is there a way to do it? Thanks in advance.

m1k1
  • 143
  • 1
  • 2
  • 13
  • Please show your expected output. – cs95 Jun 09 '19 at 00:18
  • The answer you accepted is very inefficient. With a little hint from an expected output (like I asked for), you'd get much better solutions. – cs95 Jun 09 '19 at 00:39
  • @cs95, I accepted the answer because it is a valid solution. Can you elaborate what do you mean by inefficient? As I described, it's quite obvious what I expected to get. If the model value from models dataset is in title string then put that value in the corresponding row in the model column in data dataset, otherwise, leave a placeholder. If you think that you have a better solution, feel free to provide your solution with the explanation of why you claim it's better and I will accept your answer so that people with the same problem can find the best and most efficient solution. – m1k1 Jun 09 '19 at 01:45
  • Yes, it is obvious to me and obvious to others because of the answer you accepted, but we still require questions to have a clear [mcve] that clearly shows the input and expected output (which is missing here). In terms of performance, [a list comprehension would likely be faster](https://stackoverflow.com/questions/54028199/for-loops-with-pandas-when-should-i-care). – cs95 Jun 09 '19 at 01:47
  • @cs95, thank you, I'll have that in mind for the future. However, feel free to add your solution. – m1k1 Jun 09 '19 at 01:55

1 Answers1

1
mod = models["model"]
data["model"] = data["title"].map(lambda s: next((m for m in mod if m in s), "placeholder"))
GZ0
  • 4,055
  • 1
  • 10
  • 21