I have a Pandas dataframe that looks something like this:
> print(df)
image_name tags
0 img1 class1 class2 class3
1 img2 class2
2 img3 class2 class3
3 img4 class1
How can I reclassify the tags
column such that any row with a class3
value gets assigned the string "yes" and everything else the string "no"?
I am aware that I can check for instances of a search word using the following:
df['tags'].str.contains('class3')
However, I am not sure how to integrate this into the task at hand.
The following is the intended output:
image_name tags
0 img1 yes
1 img2 no
2 img3 yes
3 img4 no
Use np.where
as:
df['tags'] = np.where(df['tags'].str.contains('class3'),'yes','no')
Or
df['tags'] = 'no'
df.loc[df['tags'].str.contains('class3'),'tags'] = 'yes'
Or
df['tags'] = ['yes' if 'class3' in s else 'no' for s in df3.tags.values]
The output for above methods:
print(df)
image_name tags
0 img1 yes
1 img2 no
2 img3 yes
3 img4 no
You can also do:
df['tags'] = df.tags.str.contains('class3').map({True:'Yes',False:'No'})
>>> df
image_name tags
0 img1 Yes
1 img2 No
2 img3 Yes
3 img4 No
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With