Reputation: 545
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 tried something 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 on how to do it?
Upvotes: 1
Views: 71
Reputation: 863541
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)
Upvotes: 2