I have two dataframe, below
JavaScript
x
14
14
1
Key_words Possiblities
2
0 ar NaN
3
1 va NaN
4
2 eb NaN
5
3 ne NaN
6
4 ke NaN
7
8
id first_name last_name email
9
0 7840 Avery Beldon abeldon0@cyberchimps.com
10
1 7840 Emilie Anton eanton1@hp.com
11
2 7840 Corine Gabey cgabey2@state.tx.us
12
3 7840 Noak Lowdyane nlowdyane3@dot.gov
13
4 9907 Yetta Kornilov ykornilov4@smugmug.com
14
I am trying to fill df[“Possibilites”] with df2[“first_name”] if key_words in df2[“first_name”] with this code:
JavaScript
1
3
1
for i in range(0,len(df["Key_words"])):
2
df["Possiblities"].loc[i]=list(df2["first_name"][df2["first_name"].str.contains(df["Key_words"].loc[i])])
3
it returns what i expect but gives a warning also:
” SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame”
What should I do instead using “for loop”? more practical or right way…
Advertisement
Answer
Use custom lambda function with generator with join for match multiple matched values, if necessary convert values to lowercase:
JavaScript
1
10
10
1
f = lambda x: ','.join(y for y in df2["first_name"] if x.lower() in y.lower())
2
df["Possiblities"] = df["Key_words"].apply(f)
3
print (df)
4
Key_words Possiblities
5
0 ar
6
1 va
7
2 eb
8
3 ne Corine
9
4 ke
10