简体   繁体   中英

Find a substring in a string

I have a dataframe with a columns 'status' I would replace the value of status column by 1 if the value of status contains the word "Won" otherwise it replace it by 0

I try like this :

s = oppty_oppline['status']
if s.find("Won") == -1:
       oppty_oppline['status']=0
else:
        oppty_oppline['status']=1

But I got this error :

AttributeError                            Traceback (most recent call last)
<ipython-input-23-7d82ff1c0e85> in <module>()
      1 s = oppty_oppline['status']
----> 2 if s.contains("Won") == -1:
      3        oppty_oppline['status']=0
      4 else:
      5         oppty_oppline['status']=1

~/anaconda3/envs/python3/lib/python3.6/site-packages/pandas/core/generic.py in __getattr__(self, name)
   3612             if name in self._info_axis:
   3613                 return self[name]
-> 3614             return object.__getattribute__(self, name)
   3615 
   3616     def __setattr__(self, name, value):

AttributeError: 'Series' object has no attribute 'contains'

Any idea please to help me to resolve this problem

Thnaks

Use Series.str.contains for boolean mask and map True/False to 1/0 by casting to integer:

oppty_oppline['status'] = oppty_oppline['status'].str.contains('Won').astype(int)

Or using numpy.where :

oppty_oppline['status'] = np.where(oppty_oppline['status'].str.contains('Won'), 0, 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM