Reputation: 107
My dataframe is
data = {
'company_name' : ['auckland suppliers', 'Octagone', 'SodaBottel','Shimla Mirch'],
'year' : [2000, 2001, 2003, 2004],
'desc' : [' auckland has some good reviews','Octagone','we shall update you','we have varities of shimla mirch'],
}
df = pd.DataFrame(data)
i tried this code
df['CompanyMatch'] = df ['company_name'] == df ['desc']
I want to print "Match" if the first word of company_name column matches with the desc column.I am confused as in where to put index [0] so that it prints this way:
> company_name desc CompanyMatch
> auckland suppliers auckland has some good reviews Match
> Octagone Octagone Match
> SodaBottel we shall update you NA
> Shimla Mirch we have varities of shimla mirch Match
Upvotes: 2
Views: 77
Reputation: 862511
You can use numpy.where
with apply
for check one column value in another by in
, axis=1
is for processing by rows:
import numpy as np
m = df.apply(lambda x: x['company_name'].lower() in x['desc'].lower(), axis=1)
df['CompanyMatch'] = np.where(m, 'Match', np.nan)
print (df)
company_name desc year CompanyMatch
0 auckland suppliers auckland has some good reviews 2000 nan
1 Octagone Octagone 2001 Match
2 SodaBottel we shall update you 2003 nan
3 Shimla Mirch we have varities of shimla mirch 2004 Match
EDIT:
For compare first word only:
m = df.apply(lambda x: x['company_name'].split()[0].lower() in x['desc'].lower(), axis=1)
df['CompanyMatch'] = np.where(m, 'Match', np.nan)
print (df)
company_name desc year CompanyMatch
0 auckland suppliers auckland has some good reviews 2000 Match
1 Octagone Octagone 2001 Match
2 SodaBottel we shall update you 2003 nan
3 Shimla Mirch we have varities of shimla mirch 2004 Match
Upvotes: 5