estebanpdl estebanpdl - 8 months ago 39
Python Question

Binomial classification using a list comprehension python

I'm working with

, and I want to add a new
with a binomial classification if a word is within a sentence.

For example:

year = [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012]
state = ['hello', 'my friend', 'hello and', 'coding', 'python', 'hello', 'who is', 'hello, friend']
data = {'year': Series(year), 'state': Series(state)}
pd = DataFrame(data)

So I tried this to get 1 or 0 if 'hello' is in item from state list.

data['hello'] = []
for item in data['state']:
if 'hello' in item:

However, is there a method te get a binomial classification using a
list comprehension

Any suggestions will be appreciated! Thanks


A possible list comprehension equivalent is:

data['hello'] = [(1 if 'hello' in item else 0) for item in data['state']]

If True/False is acceptable:

data['hello'] = ['hello' in item for item in data['state']]