0

I would like to create a new feature ["EnglishName"] for my feature passenger["Surname"] that occurs anwywhere in another dataframe consisting of a single column containing common English Surnames (surnames["Name"]).

I am struggling to find the right output.

this code gives output but it gives "False" for every Surnames, even the ones that are present in "name"

passenger["EnglishName"] = passenger["Surname"]==(surnames["name"].any())

I thought I could fix this with a lambda expression, but I have not been able to figure out the right format

passenger["EnglishName"] = passenger["Surname"].apply(lambda x: 1 if x.isin(surnames["name"].any()))

I have searched Stackoverflow for similar code, but they often have to do with SQL kind of syntax (matching two dataframes) or the data is not present in a dataframe and you can use .isin without a problem.

What is the right code to anwser this simple question? You help is greatly appreciated!

brenda89
  • 99
  • 9
  • 2
    Please include [`reproducible example`](https://stackoverflow.com/q/20109391/4985099) – sushanth Aug 29 '20 at 09:56
  • 1
    `passenger["EnglishName"] = passenger["Surname"].isin(surnames["name"].tolist())` or `passenger["EnglishName"] = passenger["Surname"].where(passenger["Surname"].isin(surnames["name"].tolist()))`? – ansev Aug 29 '20 at 09:58
  • Thank you! adding .tolist() to the code solved the problem – brenda89 Aug 29 '20 at 10:05
  • `.tolist()` works because the comparison is not made based on the series index – ansev Aug 29 '20 at 10:08

0 Answers0