2

I'm having a problem in replacing my data. I have a large dataset. lets say I have 15 attributes, the 15th attributes is Label. I want to change if the data contains botnet, it will change the whole data into "1". for example the data botnet are here or we are looking for botnet, both data will replace to "1"

I've already try using replace

x = "botnet is here and im looking for botnet"

tags = ['botnet']

for tag in tags:
    x = x.replace(tag,'')
print(x)

This code only replce word "botnet" But what I want is if the data contain botnet it will change the whole sentences to "1"

  • 2
    Possible duplicate of [How to use string.replace() in python 3.x](https://stackoverflow.com/questions/9452108/how-to-use-string-replace-in-python-3-x) – BSeitkazin Nov 01 '18 at 05:18
  • For me this sounds like you are looking for: for tag in tags: x = "1" if tag in x else x; print(x) – quant Nov 01 '18 at 05:22

3 Answers3

1
for tag in tags:
    if tag in x:
        x = "1"
        break
Michael
  • 2,344
  • 6
  • 12
  • but if i have a dataset how can i use this command to change certains collumn – Wan Nur Hidayah Ibrahim Nov 01 '18 at 05:54
  • While this might answer the authors question, it lacks some explaining words and links to documentation. Raw code snippets are not very helpful without some phrases around it. You may also find [how to write a good answer](https://stackoverflow.com/help/how-to-answer) very helpful. Please edit your answer. – hellow Nov 01 '18 at 07:46
1

Try this:

label = ['botnet is here' , 'im looking for botnet', 'just a test']
tags='botnet'
for x in label:
    if tags in x:
        label[label.index(x)]='1'
print(label)

output: only sentences contain 'botnet' are replaced

['1', '1', 'just a test']
Ishara Madhawa
  • 3,549
  • 5
  • 24
  • 42
0

You should assign string value to tags variable instead of list.
Iterate x not tags.
Try this code.

x = "botnet is here and im looking for botnet"
tags='botnet'
for tag in x.split():
    if tag==tags:
        x=tag.replace(tag,'1')
print(x)

output of this code is.

1
kaleem231
  • 357
  • 1
  • 8